Re: Magic Methods not working

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

 



On Wed, Jan 27, 2010 at 11:04 AM, Paul M Foster <paulf@xxxxxxxxxxxxxxxxx>wrote:

> 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
>
>
Paul,

I seem that you should missed the required underscore " _  "
It should the __get() but not _get().
Shall this help ?

And here is a quick test !!

[ [ [
class test
{
    public $varname = 1000;

    public function __get($name)
    {
        return $this->init($name);
    }

    public function init($name)
    {
        return new test();
    }
}

$t = new test();
$db = $t->database;
if (is_object($db))
{
    echo $db->varname;
}

] ] ]


Regards,
Eric,


--
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[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