note 50706 added to keyword.paamayim-nekudotayim
| From: | mark at branly dot com | Date: | Mon, 07 Mar 2005 22:53:34 +0000 |
| Subject: | note 50706 added to keyword.paamayim-nekudotayim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86071@lists.php.net to get a copy of this message | ||
I didn't see where this issue was addressed so in regards to this post:
\\-------\\//-------//
when using "::" operator inside class functions, you can achieve quite interesting
results. let's take this example:
class cCat {
function Miew(){
// cCat does not have a member "kind", but cDog has, and we'll use it
echo "I am ".$this->kind.", and I say MIEW\n";
// here things are even stranger: does cCat class
// support WhoAmI function? guess again...
$this->WhoAmI();
}
}
class cDog {
var $kind = "DOG";
function Bark(){
// let's make this dog act like a cat:)
cCat::Miew();
}
function WhoAmI(){
echo "Yes, I'm really ".$this->kind."!";
}
}
$dog = new cDog();
echo $dog->Bark();
outputs:
I am DOG, and I say MIEW
Yes, I'm really DOG!
The interesting thing here is that cDog is not descendant of cCat nor vice versa, but cCat was able
to use cDog member variable and function. When calling cCat::Miew() function, your $this variable is
passed to that function, remaining cDog instance!
It looks like PHP doesn't check if some class is an ancestor of the class, calling function via
'::'.
//------//\\--------\\
The problem here is not that PHP is not checking ancestry. The problem is that "$this"
refers to the calling object $dog even though it is referenced inside the cCat class definition.
Since a cCat is never instantiated, $this has not been scoped to it.
To further illustrate the point, if you never instatiate dog (or any object for that matter), $this
will never be set and when you statically call the miew function, you will get the following output:
"I am , and I say MIEW"
This flexibility of member handling, PHP does not afford to methods and as such the second line of
the miew function will generate a fatal error reporting that the method has been called from a non
object.
If you did want to take it one step further in the whole ancestry guessing issue, try instatiating
cCat and calling the miew function. You will get the same result as above *EXCEPT* the fatal error
will inform you the function has not been defined.
So, there you have it--PHP DOES NOT guess at inheritance.
----
Manual Page -- http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50706
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50706&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50706&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50706&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50706&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50706&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50706&report=yes
Search -- http://master.php.net/manage/user-notes.php