PIDZB
PIDZB

Reputation: 913

Refactoring procedural code into OOP, difficulties with scoping

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?

Upvotes: 3

Views: 551

Answers (1)

chipwilson
chipwilson

Reputation: 160

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

Upvotes: 1

Related Questions