2014年4月26日 星期六

__construct & parent::__construct

一般在繼承並建構的話,會複寫父類別, 但當子類別加上
parent::__construct();
初始子類別時,會同時調用父類別,以下是範例


<?php
 
class alpha
{
    public function __construct()
    {
        echo "hello world";
    }
}
 
class test extends alpha
{
    public function __construct()
    {
        echo "hello test";
    }
}
 
$test = new test; 
 
//output -> hello test 
?>

<?php
 
class alpha
{
    public function __construct()
    {
        echo "hello world ";
    }
}
 
class test extends alpha
{
    public function __construct()
    {
        parent::__construct();
        {
         echo "hello test";
        } 
    }
}
 
$test = new test; 
 
//output -> hello world hello test 
?>

沒有留言 :

張貼留言