NYCPHP Meetup

NYPHP.org

[nycphp-talk] OOP Question: Calling a classes function from anotherclass

Rob Marscher rmarscher at beaffinitive.com
Wed Apr 18 18:24:00 EDT 2007


> I just went to order the book on bookpool.com, but its out of stock.
> I'll wait a bit and get it soon. Thanks.
>
> - Ben

When you get the book (or sooner if you look online), you should  
check out the Singleton pattern.

If your class method can't be static (because it needs access to  
$this - which isn't available for static methods), then a Singleton  
is a good option to use if you only  need one instance of the object  
in your app.

It works basically like this:

class Error
{
   private static $_instance = null;

   public static function singleton()
   {
     if (self::$_instance === null) {
       self::$_instance = new Error();
     }

     return self::$_instance;
   }
}

Then, wherever you need to get your Error object, just call

$error = Error::singleton();


-Rob




More information about the talk mailing list