[Solar-talk] Quick thoughts on new factory() method instantiation
Paul M Jones
pmjones at ciaweb.net
Wed Jan 3 09:32:59 PST 2007
On Dec 11, 2006, at 6:25 PM, Travis Swicegood wrote:
> 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.
I have just now changed it from 'factory' to 'solarFactory' and
committed to SVN.
> 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.
*gasp*
> 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.
Only problem with using the static method is that the factory-object
won't get its own config values from the config file; I can intuit
that such a case will arise. I don't like the double-instantiation
either, but I don't see a way around it that preserves the other
expected behaviors (such as configs).
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Join the Solar community wiki! <http://solarphp.org>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the solar-talk
mailing list