dongwh1992 2016-06-26 17:52
浏览 29
已采纳

将过程代码重构为OOP,确定范围有困难

I am currently refactoring a huge switch that parses a page from procedural code to OOP.

I have some builder-classes that need a few dependencies that have scoping issues at the moment.

This is a snippet of the code before:

function parsePage($sLanguageCode) {
    $oTranslator = new translator($sLanguageCode);
    $aTranslations = $oTranslator->translations('page');
    $oBuilderClass = new builder($aTranslations);


    //... queries to get data and set pagedata and get the template file
    $oPageData = $oPage->getData();
    $aTemplateTags = $oTemplate->getTags();    
    foreach($aTemplateTags as $sTag) {

        switch($sTag) {
            case 'php':
                if(is_object($oPageData->getPhp())) {
                    include $oPageData->getPhp()->getData();
                } elseif(is_array($oPageData->getPhp())) {
                    foreach($oPageData->getPhp() as $oElement) {
                        include $oElement->getData();
                    }
                }                   
                break;
            case 'element':
                if(is_object($oPageData->getElements())) {
                    $oBuilderClass->buildElement($oPageData->getElements()->getData());
                } elseif(is_array($oPageData->getElements())) {
                    foreach($oPageData->getElements() as $oElement) {
                        $oBuilderClass->buildElement($oElement);
                    }
                }
                break;       
            //... A lot more cases here, like 20
        }
    }

    //....
}

As you can see above, there is a lot of duplicate code present and I need to retreive the data in more functions, thus I wanted to encapsulate the logic inside objects to prevent duplicate code. This is a snipped of the code in OOP:

function parsePage($sLanguageCode) {
    $oTranslator = new translator($sLanguageCode);
    $aTranslations = $oTranslator->translations('page');
    $oBuilderClass = new builder($aTranslations);

    //... queries to get data and set pagedata and get the template file
    $oPageData = $oPage->getData();
    $aTemplateTags = $oTemplate->getTags(); 
    foreach($aTemplateTags as $sTag) {
        $oPageData->outputData($sTag);

    //....   
}

The pageData class, containing all the data-objects, looks something like:

class pageData {
    protected $aPhpFragments;
    protected $aElementFragments;

    public function outputData($sTag) {
        switch($sTag) {
            case 'php':
                foreach($this->aPhpFragments as $oPhpFragment) {
                    $oPhpFragment->render();
                }
                break;
            case 'element':
                foreach($this->aElementFragments as $oElementFragment) {
                    $oElementFragment->render();
                }
                break;
        }
    }
}

Some of the data classes look like:

class phpFragment {
    private $sData;

    function render() {
        return include $oElement->sData;
    }
}

class elementFragment {
    private $sData;

    function render() {
        echo $oBuilderClass->buildElement($this->sData);
    }
}

Most of these data objects can render their content without any dependecies, but some need a few builder/data objects. Like the elementFragment class, this needs the $oBuilderClass with the translations set. I only want to create these dependecy objects once, because some are pretty big and e.g. containing alot of translations. The data objects get serialized and stored into the MySQL database.

Questions:

  1. How can I use the builder-object into my fragment-objects?
  2. Some of the objects get stored into the database, so when I use reference variables inside the new objects, these references will get stored too?
  • 写回答

1条回答 默认 最新

  • doudao1950 2016-07-05 17:06
    关注

    The dependency injection pattern would really help your scoping problems. Here is a good article on using the dependency injection pattern in PHP: http://fabien.potencier.org/what-is-dependency-injection.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看