php的继承功能还很不完善,并且很容易出错。运行下面的代码,看看结果是否是你所要的。
interface IA
{
public function Print1();
public function Print2();
//private function Pritn3(); //error
//public $m_var1; //error
};
abstract class CB
{
public abstract function Echo1();
//private abstract function Echo2(); //error
public function Echo3()
{
echo "CB::Echo3
";
echo "m_var1 = " . $this->m_var1 . "
";
echo "m_var2 = " . $this->m_var2 . "
";
echo "m_var3 = " . $this->m_var3 . "
";
echo "m_var4 = " . $this->m_var4 . "
";
echo "m_var = " . CB::m_var . "
";
echo "static m_var3 = " . CB::$m_var3 . "
";
}
public $m_var1;
private $m_var2;
public static $m_var3 = 11;
protected $m_var4;
const m_var = 10; //声明为常量
};
class CC implements IA
{
public function CC()
{
echo "constructor of CC has been called!
";
}
public function __destructor()
{
echo "destructor of CC has been called!
";
}
function Print1()
{
echo "CC::Print1()
";
}
function Print2()
{
echo "CC::Print2()
";
}
};
class CD extends CB
{
public function CD()
{
echo "constructor of CD has been called!
";
//parent::$m_var1 = 1; //error
$this->m_var1 = 1;
$this->m_var2 = 2;//因为父类中位private所以,无法直接去访问。因此,隐式的定义了一个变量
$this->m_var3 = 3;
$this->m_var4 = 4;
$this->m_var = 5;
}
function Echo1()
{
echo "CD::Echo1()
";
}
};
?>
//$objA = new IA;
//$objB = new CB;
$objC = new CC;
$objD = new CD;
$objC->Print1();
$objC->Print2();
$objD->Echo1();
$objD->Echo3();
?>
结果:
constructor of CC has been called!constructor of CD has been called!CC::Print1()CC::Print2()CD::Echo1()CB::Echo3m_var1 = 1m_var2 = m_var3 = 3m_var4 = 4m_var = 10static m_var3 = 11