Created
November 12, 2012 23:18
-
-
Save kopiro/4062740 to your computer and use it in GitHub Desktop.
Multiple inheritance on PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class GranFather | |
| { | |
| function a() | |
| { | |
| return 'GranFather.A'; | |
| } | |
| function b() | |
| { | |
| return 'GranFather.B'; | |
| } | |
| } | |
| class Father | |
| { | |
| private $parent, $extend = 'GranFather'; | |
| function __parent() | |
| { | |
| if (!$this->parent) $this->parent = new $this->extend(); | |
| return $this->parent; | |
| } | |
| function __call($foo, $args) | |
| { | |
| return call_user_func_array(array($this->__parent(), $foo), $args); | |
| } | |
| function __get($var) | |
| { | |
| return $this->__parent()->__get($var); | |
| } | |
| function a() | |
| { | |
| return 'Father.A - OVERRIDE'; | |
| } | |
| } | |
| class Child | |
| { | |
| private $parent, $extend = 'Father'; | |
| function __parent() | |
| { | |
| if (!$this->parent) $this->parent = new $this->extend(); | |
| return $this->parent; | |
| } | |
| function __call($foo, $args) | |
| { | |
| return call_user_func_array(array($this->__parent(), $foo), $args); | |
| } | |
| function __get($var) | |
| { | |
| return $this->__parent()->__get($var); | |
| } | |
| function a() | |
| { | |
| return 'Child.A - OVERRIDE AGAIN'; | |
| } | |
| } | |
| $g = new GranFather(); | |
| $f = new Father(); | |
| $c = new Child(); | |
| echo "GranFather.A: ".$g->a()."\n"; | |
| echo "GranFather.B: ".$g->b()."\n"; | |
| echo "Father.A(): ".$f->a()."\n"; | |
| echo "Father.B(): ".$f->b()."\n"; | |
| echo "Child.A(): ".$c->a()."\n"; | |
| echo "Child.B(): ".$c->b()."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment