Skip to content

Instantly share code, notes, and snippets.

@sharkpp
Created November 22, 2013 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharkpp/7601323 to your computer and use it in GitHub Desktop.
Save sharkpp/7601323 to your computer and use it in GitHub Desktop.
あるオブジェクトの親クラス(派生元クラス)のメソッドを外部から呼び出す
<?php
class C
{
private $a;
function __construct($owner)
{
$this->a = $owner;
}
public function test($v)
{
echo "" , __METHOD__ , "\n";
return call_user_func(array($this->a, 'A::test'), $v);
}
}
class A
{
private $x;
function __construct($x)
{
$this->x = $x;
}
public function test($v)
{
echo "" , __METHOD__ , "\n";
return '>>' . $this->x .':' . $v;
}
}
class B extends A
{
private $c;
function __construct($x)
{
parent::__construct($x);
$this->c = new C($this);
}
public function test($v)
{
echo "" , __METHOD__ , "\n";
return $this->c->test($v);
}
}
$b = new B(10);
echo $b->test('test') . "\n";
// --- output ---
// B::test
// C::test
// A::test
// >>10:test
// --------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment