note 104298 added to function.get-parent-class
| From: | MichaelTorpKaalund at osu1 dot php dot net | Date: | Tue, 07 Jun 2011 12:45:33 +0000 |
| Subject: | note 104298 added to function.get-parent-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180308@lists.php.net to get a copy of this message | ||
I have been playing with parent and child class, and I used this function to determine that which
one was calling an function. I for an example it if you only want your parent class to be able to
make an function call:
here is my example (by the way this example was inspired by another example fund on php.net):
<?php
class Parents {
public static $status; //This is the status
protected static $idCount;
protected $id;
function __construct() {
$this->id = ++self::$idCount;
self::$status = "tmp";
}
public function run($task) {
if(get_parent_class($this) == "") {
echo 'Command issued: '.$task.'<br>';
self::$status = $task;
}
}
public function __tostring() {
return "(".__class__.")id=".$this->id."
status=".self::$status."<br>";
}
}
class Child extends Parents {
function __construct() {
parent::__construct();
self::$status = "tmp";
echo "Child construct<br>";
}
public function __tostring() {
return "(".__class__.")id=".$this->id."
status=".self::$status."<br>";
}
}
$parent = new Parents();
echo $parent;
$child = new Child();
echo $child;
echo get_parent_class('Child')."<br>";
$parent->run("mtk");
echo $parent;
echo $child;
$child->run("mtk1");
echo $parent;
echo $child;
?>
which will produces somethink like:
(Parents)id=1 status=tmp
Child construct
(Child)id=2 status=tmp
Parents
Command issued: mtk
(Parents)id=1 status=mtk
(Child)id=2 status=mtk
(Parents)id=1 status=mtk
(Child)id=2 status=mtk
hopes it helps some one :D
----
Server IP: 69.147.83.197
Probable Submitter: 62.198.241.228
----
Manual Page -- http://www.php.net/manual/en/function.get-parent-class.php
Edit -- https://master.php.net/note/edit/104298
Del: integrated -- https://master.php.net/note/delete/104298/integrated
Del: useless -- https://master.php.net/note/delete/104298/useless
Del: bad code -- https://master.php.net/note/delete/104298/bad+code
Del: spam -- https://master.php.net/note/delete/104298/spam
Del: non-english -- https://master.php.net/note/delete/104298/non-english
Del: in docs -- https://master.php.net/note/delete/104298/in+docs
Del: other reasons-- https://master.php.net/note/delete/104298
Reject -- https://master.php.net/note/reject/104298
Search -- https://master.php.net/manage/user-notes.php