Re: Re: Class::__construct() vs. $this->__construct()

From: Date: Thu, 31 Mar 2005 18:33:10 +0000
Subject: Re: Re: Class::__construct() vs. $this->__construct()
References: 1 2 3 4  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-37026@lists.php.net to get a copy of this message
In a final response to my initial question ... It looks like the best way to have your code run properly in both PHP5 and PHP4 is to do the following: class Foo {
    function Foo()
    {
        $this->__construct();
    }
    function __construct()
    {
        parent::__construct(); // not needed unless class extends from a parent
    }
} I only asked because I saw XML_Parser was calling XML_Parser::__construct() from function XML_Parser(). I though $this->__construct() would be safer. A bug possibly? --Joe -- Joseph C. Stump joe@joestump.net http://www.joestump.net On Mar 31, 2005, at 8:47 AM, Hans L wrote:
Justin Patrin wrote:
Use $this->__construct() in the PHP 4 class constructor only. By Calling "Foo::__construct()" you're calling a static method, unrelated to the current instance. "parent::" is magic so the scope remains within the current instance.
As said earlier, this is incorrect. Both will do the same thing if Foo is the name of the class. $this stays set wherever you go once you're in a class method.
... unless you're using PHP5 in which case you get:
     "Fatal error:  Using $this when not in object context"
I'm assuming the use of $this->__construct() is to be compatible w/ PHP5 ... and so I'd imagine that you'd want your code not to completely break under PHP5 (!) -Hans


« previous php.pear.dev (#37026) next »