note 60898 added to keyword.extends
| From: | tilman dot schroeder at gmx dot de | Date: | Thu, 19 Jan 2006 19:24:19 +0000 |
| Subject: | note 60898 added to keyword.extends | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102108@lists.php.net to get a copy of this message | ||
Hi...
as alan hogan pointed out it is possible to inherit a class from multiple other classes but notice
that php does not always give you the right name of the parent class if you are deriving from
multiple classes (tested on php 4.4.0):
<?php
header("Content-Type: text/plain");
class three {
function three() {
echo("constructor of three\n");
} // end function three
} // end class three
class two extends three {
function two() {
echo("constructor of two\n");
} // end function two
function two_get_parent_class_name() {
echo("i am two and my parent is: ".get_parent_class($this)."\n");
} // end one_function get_parent_class_name()
} // end class two
class one extends two {
function one() {
echo("constructor of one\n");
} // end function one
function one_get_parent_class_name() {
echo("i am one and my parent is: ".get_parent_class($this)."\n");
} // end one_function get_parent_class_name()
} // end class one
$one=new one();
$one->two();
$one->three();
$one->one_get_parent_class_name();
$one->two_get_parent_class_name();
/*
* will print out:
*
* constructor of one
* constructor of two
* constructor of three
* i am one and my parent is: two
* i am two and my parent is: two // should print out "my parent is: three"
*/
?>
So be careful when dealing with parents within classes ...
However, if you are using parent:: it will give you access to BOTH parent classes:
<?php
header("Content-Type: text/plain");
class three {
function three() {
echo("three\n");
}
}
class two extends three {
function two() {
echo("two\n");
}
}
class one extends two {
function one() {
echo("one\n");
parent::two();
parent::three();
}
}
one::one();
/*
* will print out:
* one
* two
* three
*/
?>
----
Server IP: 217.160.72.57
Probable Submitter: 80.142.216.110
----
Manual Page -- http://www.php.net/manual/en/keyword.extends.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60898
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60898&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60898&report=yes
Search -- http://master.php.net/manage/user-notes.php