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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题