我可以从其他类导入方法而不使用它们的“扩展”继承吗?
class Foo
{
public function fooMethod() {
return 'foo method';
}
}
class Too
{
public function tooMethod() {
return 'too method';
}
}
class Boo
{
public $foo;
public $too;
public function __construct()
{
$this->foo = new Foo();
$this->too = new Too();
}
}
用法,
$boo = new Boo();
var_dump($boo->foo->fooMethod()); // string 'foo method' (length=10)
var_dump($boo->too->tooMethod()); // string 'too method' (length=10)
var_dump($boo->fooMethod()); // Fatal error: Call to undefined method Boo::fooMethod() in
var_dump($boo->tooMethod()); //Fatal error: Call to undefined method Boo::tooMethod() in
理想情况下,
var_dump($boo->fooMethod()); // string 'foo method' (length=10)
var_dump($boo->tooMethod()); // string 'too method' (length=10)
可能吗?
编辑:
我知道我可以像这样实现它,
class Boo
{
public $foo;
public $too;
public function __construct()
{
$this->foo = new Foo();
$this->too = new Too();
}
public function fooMethod() {
return $this->foo->fooMethod();
}
public function tooMethod() {
return $this->too->tooMethod();
}
}
但我希望在不重新输入方法的情况下导入方法。可能吗?