Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created November 12, 2012 23:18
Show Gist options
  • Select an option

  • Save kopiro/4062740 to your computer and use it in GitHub Desktop.

Select an option

Save kopiro/4062740 to your computer and use it in GitHub Desktop.
Multiple inheritance on PHP
<?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