Re: $this scope (was T_PAAMAYIM_NEKUDOTAYIM)

From: Date: Tue, 17 Dec 2002 03:26:48 +0000
Subject: Re: $this scope (was T_PAAMAYIM_NEKUDOTAYIM)
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-92549@lists.php.net to get a copy of this message
It would be usefull to have a direction on this, - It's come up a number of times in bug reports. It has a number of uses (primarily breaking large classes up, into smaller components..), and a clearer way to write code than using a huge inheritance tree.. - It may not be correct OO code, by the book, but it is usefull in certian situations. It can be regarded as a feature, but if it is going to change at some point in the future, it's better to make that very clear now, rather than get a big backlash when we change it, and more people have worked out this little trick.... (and start to rely on it...) Technically I guess overload is the real way it should be done in ZE2..... Regards Alan Brad LaFountain wrote:
--- Bertrand Mansion <bmansion@mamasam.com> wrote:
<rodif_bl@yahoo.com> wrote :
--- Bertrand Mansion <bmansion@mamasam.com> wrote:
     
<pollita@php.net> wrote?:
       
$classname::method() === call_user_func(array($classname,'method'));
         
No. $classname::method() is illegal and is in no case === to call_user_func(array($classname,'method')) as you state it. Class foo { function bar() {
       echo $this->param;
} } Class MyObject { var $param; function MyObject($param) {
       $this->param = $param;
       foo::bar();
} } $obj = new MyObject('out of this'); will print 'from MyObject'.
       
This SHOULDN'T print 'out of this', This seems like a bug, and you
     
SHOULDN'T
depend on this functionality. The scope of $this inside class foo method
     
bar,
Should be to foo::bar NOT MyObject::MyObject. Which if my memory serves me right $this inside a static method will just be the string of the current class?
     
Hi Brad, Are you sure about that ? The manual states that this is the normal behaviour which I find very handy and useful in many cases. I see a lot of possibilities with this feature, for instance a way to develop some kind of plug-ins for existing classes without having to extend them. Where in the manual do you see this?
This can probably lead to some dangerous code but this goes far beyond my knowledge.
besides dangerous code if you find yourself doing this, you probally aren't designing it correct. Your creating a static method of a class to access local properties of another class thru the use of $this. If you want a way to access member variables from a class in a static method, pass the object refrence into the method. Class foo {
    function bar($obj) {
        echo $obj->param;
        /* $this should referr to the foo refrence, not another class refrence
*/
    }
} Class MyObject {
    var $param;
    function MyObject($param) {
        $this->param = $param;
        foo::bar($this);
} } - Brad __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com

« previous php.dev (#92549) next »