oo design question - how to avoid a factory

From: Date: Fri, 17 Dec 2004 17:39:36 +0000
Subject: oo design question - how to avoid a factory
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-35136@lists.php.net to get a copy of this message
After reading through the http://www.php.net/manual/en/language.oop.newref.php page about references in constructors, I ask to all of you, how do you avoid using a factory method in PHP4? Here is my code in question (the abridged version): class Structures_DataGrid extends Structures_DataGrid_Renderer {
    function Structures_DataGrid($limit = null, $page = 1,
                                 $renderer = DATAGRID_RENDER_TABLE)
    {
        parent::Structures_DataGrid_Renderer($renderer, $limit, $page);
    }
} class Structures_DataGrid_Renderer extends Structures_DataGrid_Core {
    var $renderer;
    function Structures_DataGrid_Renderer($renderer = DATAGRID_RENDER_TABLE, $limit = null, $page = 1)
    {
        if (PEAR::isError($this->setRenderer($renderer))) {
            $this->setRenderer(DATAGRID_RENDER_TABLE);
        }
        parent::Structures_DataGrid_Core($limit, $page);
        // Automatic handling of GET/POST/COOKIE variables
        $this->_parseHttpRequest();
    }
    function setRenderer($renderer)
    {
        $class = 'Structures_DataGrid_Renderer_' . $renderer;
        $file = 'Structures/DataGrid/Renderer/' . $renderer . '.php';
        if (@include_once($file)) {
            $this->renderer = new $class($this);
        } else {
            return new PEAR_Error('Invalid renderer');
        }
        return true;
    }
} The problem is, the renderer has no values in the datagrid variable upon rendering because of the PHP4 issue of creating a reference during construction. Is their anyway of avoiding this? Could using a factory instead of a constructor be confusing to the non-savvy user? Thanks for any advice Andrew

« previous php.pear.dev (#35136) next »