©
This document uses PHP Chinese website manual Release
继承已为大家所熟知的一个程序设计特性,PHP 的对象模型也使用了继承。继承将会影响到类与类,对象与对象之间的关系。
比如,当扩展一个类,子类就会继承父类所有公有的和受保护的方法。除非子类覆盖了父类的方法,被继承的方法都会保留其原有功能。
继承对于功能的设计和抽象是非常有用的,而且对于类似的对象增加新功能就无须重新再写这些公用的功能。
Note:
除非使用了自动加载,否则一个类必须在使用之前被定义。如果一个类扩展了另一个,则父类必须在子类之前被声明。此规则适用于类继承其它类与接口。
Example #1 继承示例
<?php
class foo
{
public function printItem ( $string )
{
echo 'Foo: ' . $string . PHP_EOL ;
}
public function printPHP ()
{
echo 'PHP is great.' . PHP_EOL ;
}
}
class bar extends foo
{
public function printItem ( $string )
{
echo 'Bar: ' . $string . PHP_EOL ;
}
}
$foo = new foo ();
$bar = new bar ();
$foo -> printItem ( 'baz' ); // Output: 'Foo: baz'
$foo -> printPHP (); // Output: 'PHP is great'
$bar -> printItem ( 'baz' ); // Output: 'Bar: baz'
$bar -> printPHP (); // Output: 'PHP is great'
?>
[#1] akashwebdev at gmail dot com [2015-07-01 05:59:16]
The Idea that multiple inheritence is not supported is correct but with tratits this can be reviewed.
for e.g.
<?php
trait custom
{
public function hello()
{
echo "hello";
}
}
trait custom2
{
public function hello()
{
echo "hello2";
}
}
class inheritsCustom
{
use custom, custom2
{
custom2::hello insteadof custom;
}
}
$obj = new inheritsCustom();
$obj->hello();
?>
[#2] ghoucine at gmail dot com [2014-08-26 20:48:22]
For multiple single inheretance to work the order of class definition is very important if you're going up more than two levels in inheretence.
This won't work:
<?php
class A extends B {}
class B extends C {}
class C {}
$A = new A;
?>
Running the code above will spit a fatal error (Fatal Error:Class 'B' not found)
A change in class definition order will fix this:
<?php
class A extends B {}
class C {}
class B extends C {}
$A = new A;
?>
If you're goin up just one level in inhertance though there shouldn't be any problem. So this code works too:
<?php
class A extends B {}
class B {}
$A = new A;
?>
I have no explanation as to why this is the case though.
[#3] msg2maciej at aol dot com [2011-01-28 16:08:45]
PHP supports single class inheritance. My bare idea on accessing protected methods with power of abstracts and sort of "multi-class inheritance SIMULATION":
<?php
error_reporting(E_ALL);
abstract class Base {
abstract protected function __construct ();
abstract protected function hello_left ();
abstract protected function hello_right ();
}
abstract class NotImplemented_Left extends Base {
protected function hello_right () {
echo 'well, wont see that'; }}
abstract class NotImplemented_Right extends Base {
protected function hello_left () {
echo 'well, wont see that'; }}
class Left extends NotImplemented_Left {
protected function __construct () { # limited visibility, no access from "outside"
echo __CLASS__.'::protected __construct'. "\n"; }
protected function hello_left () { # limited visibility, no access from "outside"
echo 'protected hello_left in ' . __CLASS__ . "\n"; }}
class Right extends NotImplemented_Right {
protected function __construct () { # limited visibility, no access from "outside"
echo __CLASS__.'::protected __construct'. "\n"; }
protected function hello_right () { # limited visibility, no access from "outside"
echo 'protected hello_right in ' . __CLASS__ . "\n"; }
protected function hello_left () {
echo "wont see that, and easy to get rid of it from here\n"; }}
class Center extends Base {
private $left;
private $right;
public function __construct () {
echo 'welcome in ' . __CLASS__ . "\n";
echo 'Center::'; $this->left = new Left;
echo 'Center::'; $this->right = new Right;
echo " oh and\n";
$this->hello_left();
$this->hello_right();
}
public function hello_left () { # calling class Left
echo __CLASS__.'::'; $this->left->hello_left(); }
public function hello_right () { # calling class Right
echo __CLASS__.'::'; $this->right->hello_right(); }
}
$c = new Center;
?>
Produces:
welcome in Center
Center::Left::protected __construct
Center::Right::protected __construct
oh and
Center::protected hello_left in Left
Center::protected hello_right in Right
[#4] OZ [2010-11-14 15:24:19]
Model for Mixins pattern:
<?php
interface IMixinsCaller
{
public function __mixin_get_property($property);
public function __mixin_set_property($property, $value);
public function __mixin_call($method, $value);
}
abstract class MixinsCaller implements IMixinsCaller
{
protected $mixins = array();
public function __call($name, $arguments)
{
if (!empty($this->mixins))
{
foreach ($this->mixins as $mixin)
{
if (method_exists($mixin, $name))
{
return call_user_func_array(array($mixin, $name), $arguments);
}
}
}
trigger_error('Non-existent method was called in class '.__CLASS__.': '.$name, E_USER_WARNING);
}
public function __mixin_get_property($property)
{
if (property_exists($this, $property))
{
return $this->$property;
}
trigger_error('Non-existent property was get in class '.__CLASS__.': '.$property, E_USER_WARNING);
}
public function __mixin_set_property($property, $value)
{
if (property_exists($this, $property))
{
return $this->$property = $value;
}
trigger_error('Non-existent property was set in class '.__CLASS__.': '.$property, E_USER_WARNING);
}
public function __mixin_call($method, $value)
{
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $value);
}
trigger_error('Non-existent method was called in class '.__CLASS__.': '.$method, E_USER_WARNING);
}
public function AddMixin($mixin)
{
$this->mixins[] = $mixin;
}
}
abstract class Mixin
{
private $parent_object;
public function __construct(IMixinsCaller $parent_object)
{
$this->parent_object = $parent_object;
}
public function __get($property)
{
return $this->parent_object->__mixin_get_property($property);
}
public function __set($property, $value)
{
return $this->parent_object->__mixin_set_property($property, $value);
}
public function __call($method, $value)
{
return $this->parent_object->__mixin_call($method, $value);
}
}
?>
[#5] strata_ranger at hotmail dot com [2010-09-19 09:37:58]
I was recently extending a PEAR class when I encountered a situation where I wanted to call a constructor two levels up the class hierarchy, ignoring the immediate parent. In such a case, you need to explicitly reference the class name using the :: operator.
Fortunately, just like using the 'parent' keyword PHP correctly recognizes that you are calling the function from a protected context inside the object's class hierarchy.
E.g:
<?php
class foo
{
public function something()
{
echo __CLASS__; // foo
var_dump($this);
}
}
class foo_bar extends foo
{
public function something()
{
echo __CLASS__; // foo_bar
var_dump($this);
}
}
class foo_bar_baz extends foo_bar
{
public function something()
{
echo __CLASS__; // foo_bar_baz
var_dump($this);
}
public function call()
{
echo self::something(); // self
echo parent::something(); // parent
echo foo::something(); // grandparent
}
}
error_reporting(-1);
$obj = new foo_bar_baz();
$obj->call();
// Output similar to:
// foo_bar_baz
// object(foo_bar_baz)[1]
// foo_bar
// object(foo_bar_baz)[1]
// foo
// object(foo_bar_baz)[1]
?>
[#6] jackdracona at msn dot com [2010-04-14 20:53:15]
Here is some clarification about PHP inheritance ?C there is a lot of bad information on the net. PHP does support Multi-level inheritance. (I tested it using version 5.2.9). It does not support multiple inheritance.
This means that you cannot have one class extend 2 other classes (see the extends keyword). However, you can have one class extend another, which extends another, and so on.
Example:
<?php
class A {
// more code here
}
class B extends A {
// more code here
}
class C extends B {
// more code here
}
$someObj = new A(); // no problems
$someOtherObj = new B(); // no problems
$lastObj = new C(); // still no problems
?>
[#7] php at sleep is the enemy dot co dot uk [2010-04-07 02:36:31]
Here's fun, an attempt to make some degree of multiple inheritance work in PHP using mixins. It's not particularly pretty, doesn't support method visibility modifiers and, if put to any meaningful purpose, could well make your call stack balloon to Ruby-on-Rails-esque proportions, but it does work.
<?php
abstract class Mix {
protected $_mixMap = array();
public function __construct(){
$this->_mixMap = $this->collectMixins($this);
}
public function __call($method, $args){
// doesn't pass scope
//return call_user_func_array(array($className, $method), $args);
// Error: Given object is not an instance of the class this method was declared in
//$method = new ReflectionMethod($className, $method);
//return $method->invokeArgs($this, $args);
$payload = $this->buildMixinPayload($this->_mixMap, $method, $args);
if(!$payload) throw new Exception('Method ' . $method . ' not found');
list($mixinMethod, list($method, $args)) = $payload;
return $this->$mixinMethod($method, $args);
}
protected function collectMixins($class){
static $found = array();
static $branch = array();
if(empty($branch)) $branch[] = get_class($this);
$mixins = array();
foreach(array_reverse(get_class_methods($class)) as $method){
if(preg_match('/^mixin(\w+)$/', $method, $matches)){
$className = $matches[1];
if(in_array($className, $branch))
throw new Exception('Circular reference detected ' . implode(' > ', $branch) . ' > ' . $className);
if(!in_array($className, $found)){
if(!class_exists($className)) throw new Exception('Class ' . $className . ' not found');
// populate props from mixin class
foreach(get_class_vars($className) as $key => $value){
if(!property_exists($this, $key)) $this->$key = $value;
}
$found[] = $branch[] = $className;
$mixins[$className] = $this->collectMixins($className);
}
$branch = array(get_class($this));
}
}
return $mixins;
}
protected function buildMixinPayload($mixins, $method, $args){
foreach($mixins as $className => $parents){
$mixinMethod = 'mixin' . $className;
if(method_exists($className, $method)) return array($mixinMethod, array($method, $args));
if(!empty($parents) && $return = $this->buildMixinPayload($parents, $method, $args)){
return array($mixinMethod, $return);
}
}
return false;
}
}
?>
[#8] jarrod at squarecrow dot com [2009-10-27 06:01:45]
You can force a class to be strictly an inheritable class by using the "abstract" keyword. When you define a class with abstract, any attempt to instantiate a separate instance of it will result in a fatal error. This is useful for situations like a base class where it would be inherited by multiple child classes yet you want to restrict the ability to instantiate it by itself.
Example........
<?php
abstract class Cheese
{
//can ONLY be inherited by another class
}
class Cheddar extends Cheese
{
}
$dinner = new Cheese; //fatal error
$lunch = new Cheddar; //works!
?>