NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing a method from one class in another

Chris Weyl cweyl at alumni.drew.edu
Wed Jan 18 13:38:37 EST 2006


On 1/18/06, Kenneth Downs <ken at secdat.com> wrote:
> >
> > I guess my options are:
> >
> > - set a global variable, which has to be passed to each method
>
> By the KISS principle, this is the simplest and is also effective.
> Therefore any more complicated solution would have to be defended, no?

Not always.  For a very simple app, maybe...  but let's say you start
using a php-based library, say, something PEAR or otherwise, that also
uses a db connection (or other common object/var that needs wide
access), that also uses a global called $db, or $dbh, or $file, or... 
This kind of bug can be subtle and hard to track down, particularly if
the person maintaining the code is no longer the person who wrote it.

Also, using a singleton with a static accessor allows checks to be
implemented once.  The example functions show that you can check for a
null object in one place; implementing other checks/code in there is
also possible.

                                    -Chris



More information about the talk mailing list