[Solar-talk] Quick thoughts on new factory() method instantiation

Travis Swicegood development at domain51.com
Mon Dec 11 16:25:25 PST 2006


Paul M Jones wrote:
> Of the remaining two, I like solarFactory() better.  The property  
> idea seems a bit too loose, even for me.
>   

That was my thought on it too.  Just to look at the code, you'd never be 
sure what it's calling. 


> Having said all that, can we talk about the specific implementation  
> issue you were running into?  It may be that "factory()" (vice  
> "solarFactory()") is still viable.

The issue I was running is instantiating a factory() with this signature:

public function factory($type, $config) {
    $type = 'Vendor_Great_' . $type;
    return Solar::factory($type, $config);
}

Obviously, the drawback here is that Solar::factory() doesn't pass any 
arguments in to the individual factory() call.  That could be handled by 
storing the those variables inside the config of the Factory object, but 
then my config array either has two layers to it:

$config = array(
    'type' => 'foo',
    'config' => array(
       'real' => 'values'
    )
);

Or I have to mirror the config for the object the Factory is 
instantiating which isn't a good option in this case because my Factory 
object doesn't really know anything about the objects its creating, just 
how to locate them and create them.

An alternative here that would work nicely is to pass $config in to the 
factory() method.  Make sure you're sitting down for this, that would 
actually be the best route to go, but only if you called factory() 
statically.  I believe the point for this change was to speed up the 
Facade objects by removing the double-method call for every actual 
call.  If that's the case, you've left the some memory/speed on the 
table by not calling factory() as a static method as PHP stores all 
objects in memory until it dies.  Something like this would work quite 
nicely:

function factory($class, $config = null)
{
    Solar::loadClass($class);
   
    // is it an object factory?
    if (in_array('solarFactory', get_class_methods($class)) {
        // yes, return the class from the object factory
        return $class::solarFactory($config);
    }
   
    // a regular object, return via normal instantiation
    $obj = new $class($config);
    return $obj;
}

That makes the solarFactory() method assume all responsibility for creation.

-Travis


More information about the solar-talk mailing list