Created
November 22, 2013 15:04
-
-
Save sharkpp/7601323 to your computer and use it in GitHub Desktop.
あるオブジェクトの親クラス(派生元クラス)のメソッドを外部から呼び出す
This file contains 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 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