[Solar-talk] when to create object?
Paul M Jones
pmjones at solarphp.com
Fri Aug 25 06:52:21 PDT 2006
On Aug 21, 2006, at 2:31 PM, Antti Holvikari wrote:
> 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.
Antti has the right answer here: in short, "it depends." I do both too.
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the solar-talk
mailing list