Re: Magic Methods not working
| From: | Paul M Foster | Date: | Wed, 27 Jan 2010 03:45:35 +0000 |
| Subject: | Re: Magic Methods not working | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301526@lists.php.net to get a copy of this message | ||
On Tue, Jan 26, 2010 at 07:21:34PM -0800, Daevid Vincent wrote:
> > -----Original Message-----
> > From: Paul M Foster [mailto:paulf@quillandmouse.com]
> > Sent: Tuesday, January 26, 2010 7:05 PM
> > To: php-general@lists.php.net
> > Subject: [PHP] Magic Methods not working
> >
> > I have a class which instantiates other classes, and has a
> > magic method
> > like this:
> >
> > function _get($classname)
> > {
> > return $this->instantiate($classname);
> > }
> >
> > Obviously, the class also has an instantiate method which does what it
> > says.
> >
> > Here's the problem: When I call the instantiate method, it works fine,
> > like this:
> >
> > $db = $sc->instantiate('database');
> >
> > But when I do the following, it *doesn't* work:
> >
> > $db = $sc->database;
> >
> > In fact it does not call the instantiate() method; I've placed a print
> > statement in the instantiate() method which fires at then end of the
> > routine. That statement doesn't fire with the above code.
> >
> > The docs on magic methods are pretty slim, so I'm not sure what it is
> > I'm missing. Can someone enlighten me?
> >
> > Paul
>
> http://us2.php.net/manual/en/language.oop5.magic.php
>
> There are TWO underscores needed, not one.
>
> __get()
>
> Also, just to save you some time/grief, you might consider using my awesome
> routines:
>
> Put these in your base.class.php and extend it for all other classes.
> Magic.
Oh no. I can't use those. The braces are in all the wrong places! (All
praise be to K&R...) ;-}
Paul
--
Paul M. Foster