Re: oo design question - how to avoid a factory

From: Date: Fri, 17 Dec 2004 19:03:02 +0000
Subject: Re: oo design question - how to avoid a factory
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-35137@lists.php.net to get a copy of this message
On Fri, 17 Dec 2004 12:39:36 -0500, Andrew Nagy <andrew.nagy@villanova.edu> wrote: > 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? > I don't see a factory as being too confusing, as long as you *always* use the factory in examples and make it clear in the docs that you should (must) use the factory. If you want to avoid the factory you can always make the user call setRenderer() after a 'new' call. Or call it internally whenever you need to use the renderer instead of instantiating it at "new" time. -- Justin Patrin

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