我有这个基类:
class Base
{
public $extA;
public $extB;
function __construct()
{
}
public function Init()
{
$this->extA = new ExtA();
$this->extB = new ExtB( $this );
}
public function Test()
{
return 'Base Test Here!';
}
}
扩展基类的 ExtA 类
class ExtA extends Base
{
public function Test()
{
return 'ExtA Test Here!';
}
}
类 ExtB 也扩展了基类
class ExtB extends Base
{
private $base;
public function __construct( $base )
{
$this->base = $base;
}
public function Test()
{
return 'ExtB calling ExtA->Test()::' . $this->base->extA->Test();
}
}
$base = new Base();
$base->Init();
var_dump( $base->Test() );
var_dump( $base->extA->Test() );
var_dump( $base->extB->Test() );
我尝试从 ExtB 调用 ExtA 类 Test() 函数,ExtA 和 ExtB 都在扩展 Base 类。我的问题是:这样可以吗,还是有更好、更快的解决方案?
扩展也是必要的吗?或者就像这样
class ExtA
{
...
}
class ExtB
{
...
}
谢谢!