[Solar-talk] when to create object?

Antti Holvikari anttih at gmail.com
Mon Aug 21 12:31:24 PDT 2006


On 8/18/06, Jeff Surgeson <solar at 3hex.com> wrote:
> Which of the following is correct, or the better way, is one more efficient
> than the other, or is it six of one!
>
> In base class create the object so it is available to all extended classes:
>
> protected $_products;
> $this->_products = Solar::factory('Vendor_Model_Products');
>
> and then in any extended class use it as normal:
>
> $this->_products->fetch()
>
> Or is it more efficient to only create the object when needed in each method
> that might need it and repeat this whenever.
>
> $products = Solar::factory('Vendor_Model_Products');
> $product = $products->fetch()

I do both with my own code. For me it usually comes down to a question
how often will I be using that object. If it's almost in every method,
then yes, make it available as a class property. You'll have to be the
judge to decide if it will be useful and as simple/fast as you want it
to be. If you need  it *everywhere* then Solar::registry() is your
friend.

Hope this helps.

-- 
Antti Holvikari <http://phphalo.com>


More information about the solar-talk mailing list