2013-04-30 133 views
0
Class a { 
     public function __construct($a){ 
       $this->age = $a; 
     } 
} 

Class b extends a { 
     public function printInfo(){ 
       echo 'age: ' . $this->age . "\n"; 
     } 
} 

$var = new b('age'); 
$var->printInfo(); 

我明白這段代碼的工作原理,但是有可能將參數傳遞給類和父類的構造函數嗎?是否可以將參數傳遞給類和父類構造函數?

下面我試圖導致錯誤

Class a { 
     public function __construct($a){ 
       $this->age = $a; 
     } 
} 

Class b extends a { 
     public function __construct($name){ 
       $this->name = $name; 
     } 
     public function printInfo(){ 
       echo 'name: ' . $this->name . "\n"; 
       echo 'age: ' . $this->age . "\n"; 
     } 
} 

$var = new b('name', 'age'); 
$var->printInfo(); 
?> 
+0

'父:: __結構();'B類將調用構造函數的類。所以試試'公共函數__construct($ name,$ age){$ this-> name = $ name;父:: __結構($歲); }' – Waygood 2013-04-30 07:07:21

回答

3

是的,你只需要使用parent::__construct()方法。

像這樣:

class a{   

    /** 
    * The age of the user 
    * 
    * @var integer 
    */ 
    protected $age; 

    function __construct($a){ 
     $this->age = $a; 
    } 

} 

class b extends a{   

    /** 
    * The name of the user 
    * 
    * @var string 
    */ 
    protected $name; 

    function __construct($name,$age){ 
     // Set the name 
     $this->name = $name; 

     // Set the age 
     parent::__construct($age); 
    } 

    public function printInfo(){ 
     echo 'name: ' . $this->name . "\n"; 
     echo 'age: ' . $this->age . "\n"; 
    } 
} 

$var = new b('name','age'); 
$var->printInfo(); 

只需確保變量設置爲公共或保護!

+0

重寫給它們不同方法簽名的方法是一個壞主意。 'public function __construct($ args = array()){parent :: __ construct($ args); ''是避免這種情況的一個例子。 – AD7six 2013-04-30 07:20:34

0

可以傳遞價值的父類的構造,但你正在做的方式是錯誤的,

$var = new b('name', 'age'); 

這是因爲如果在子類接受它的構造函數中有兩個參數,但實際上它只有一個參數。

你可以傳遞參數給父類的構造是這樣的

parent::__construct($var); 

因此改變你的B類本

Class b extends a { 
    public function __construct($name, $age){ 
      $this->name = $name; 
      parent::__construct($age); 
    } 
    public function printInfo(){ 
      echo 'name: ' . $this->name . "\n"; 
      echo 'age: ' . $this->age . "\n"; 
    } 
} 
0

是可以傳遞參數給類以及父類

Class a { 
     public function __construct($age){ 
       $this->age = $a; 
     } 
} 

Class b extends a { 
     public function __construct($name,$age){ 
       parent::__construct($age); 
       $this->name = $name; 
     }  
} 

$var = new b('name', 'age'); 

?> 
0

只需調用父:: __在孩子構建。例如

class Form extends Tag 
{ 
    function __construct() 
    { 
     parent::__construct(); 
     // Called second. 
    } 
} 
0

這是怎麼應該去:

<?php 
class a { 
    private $age; 
    public function __construct($age){ 
      $this->age = $age; 
    } 
    public function getAge() 
    { 
     return $this->age; 
    } 
} 

class b extends a { 
    private $name; 

    public function __construct($age, $name){ 
     parent::__construct($age); 
      $this->name = $name; 
    } 
    public function printInfo(){ 
      echo 'name: ' . $this->name . "\n"; 
      echo 'age: ' . $this->getAge() . "\n"; 
    } 
} 

$b = new b(20, "Bob"); 
$b->printInfo(); 
?> 
相關問題