note 42769 added to ref.classobj
| From: | HOC at rn2 dot php dot net | Date: | Fri, 28 May 2004 13:40:53 +0000 |
| Subject: | note 42769 added to ref.classobj | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70241@lists.php.net to get a copy of this message | ||
to pillepop2003
Why do u want to know the classname of an non-existant object?
The only possible explanation for this question seems to me u want to know the class before u
instantiate the object. Well, this is of no use since u always instantiate a class of ur choice.
When the class is instantiated into an object u can find the class of the object by means of
get_class(). This is all u need. In case of inheritance u can use get_class($this) to get the class
of the instantiated object. Now u can differentiate according to which class the object belongs to.
e.g.:
<?php
class A{
function A(){
$class_of_this = get_class($this);
echo 'Object is an instance of class '.$class_of_this.' which is the ';
if(strcmp($class_of_this,'A')==0)
echo 'parent-class';
else if(strcmp($class_of_this,'B')==0)
echo 'child-class';
echo ".\n";
}
}
class B extends A{
function B(){
$this->A();
}
}
$object1 = new A();
$object2 = new B();
?>
When u run this code-snippet the output will be:
Object is an instance of class A which is the parent-class.
Object is an instance of class B which is the child-class.
----
Manual Page -- http://www.php.net/manual/en/ref.classobj.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42769
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42769&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42769&report=yes
Search -- http://master.php.net/manage/user-notes.php