note 60898 deleted from keyword.extends by danbrown
| From: | danbrown@php.net | Date: | Wed, 17 Jun 2009 19:03:08 +0000 |
| Subject: | note 60898 deleted from keyword.extends by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157023@lists.php.net to get a copy of this message | ||
Note Submitter: tilman dot schroeder at gmx dot de
----
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
*/
?>