Re: How can I do this -- method chaining

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 30, 2008 5:56 AM, Stut <stuttle@xxxxxxxxx> wrote:

> Nathan Nobbe wrote:
> You posted a singleton pattern.


no, what i posted was a simple factory pattern.
if you invoke it twice there will be 2 instances of Test in memory,
eg. not singleton.
$a = Test::getInstance();
$b = Test::getInstance();


> That means that from the moment you call
> the static method until the end of the script that object exists. That's
> probably fine for web-based scripts that don't run for long, but I live
> in a world where classes often get used in unexpected ways so I tend to
> write code that's efficient without relying on the environment it's
> running in to clean it up.


i usually only need to do cleanup in cli scripts that batch large amounts of
data.  this is my practical experience anyway.


> This would be my implementation...
>
> <?php
> class Test {
>     public static function doSomething() {
>         $o = new Test();
>         $o->_doSomething();
>     }
>
>     protected function _doSomething() {
>         // I'm assuming this method is fairly complex, and involves
>         // more than just this method, otherwise there is no point
>         // in creating an instance of the class, just use a static
>         // method.
>     }
> }
> Test::doSomething();
> ?>
>
> Of course this is just based on what the OP said they wanted to do. If
> there is no reason to create an instance of the object then don't do it.


well you are still creating an instance of the object.  the only difference
is
you are forcing it the ref count to 0 by assigning the instance to a local
local variable.

It's fairly likely that I'd actually just use a static method here,


both your and my code use static methods.  it sounds to me like you are
using the term 'static method' to mean a static method that has a variable
with a reference to an instance of the class that it is a member of.  which
is obviously a particular use of a static method, and therefore a bad
practice
imho.  not the technique, mind you, the label of 'static method' for the
technique.


-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux