2015-02-08 36 views
-2

哎我是相當陌生的oop,同時學習我遇到了一個問題,折磨我超過2個小時。php parrent :: __構建和oop

請問能告訴我爲什麼this-> flavor沒有得到"grape"的價值?

<?php 
class Product{ 
    public $name = "default-name"; 
    public $price = 50; 
    public $desc = "default_description"; 

    function __construct ($jemali, $zviadi, $chuuch){ 
     $this->name=$jemali; 
     $this->price=$zviadi; 
     $this->desc=$chuuch; 
    } 

    public function getInfo(){ 
     return "product name:".$this->name; 
    } 
} 

class Soda extends Product { 
    public $flavor="default flavor"; 

    function __consturct($jemali, $zviadi, $chuuch, $lavor){ 

     parent::__construct($jemali, $zviadi, $chuuch); 
     $this->flavor=$lavor; 
    } 

    public function getInfo(){ 
     return "product name:".$this->name." flavor ".$this->flavor; 
    } 
} 

//$shirt = new Product("miriani", 10, "magari"); 
$soda = new Soda("jemala", 12, "chuchuka", "grape"); 
//echo $shirt->getInfo(); 
echo "<br />"; 
echo $soda->getInfo(); 
?> 

輸出產品名稱:jemala味味默認

+3

檢查拼寫。 '__consturct'與'__construct'不一樣。 – DCoder 2015-02-08 16:49:12

回答

1

只是一個小錯誤 - 的__construct拼寫錯誤。使用下面

<?php 
class Product{ 
    public $name = "default-name"; 
    public $price = 50; 
    public $desc = "default_description"; 

    function __construct ($jemali, $zviadi, $chuuch){ 
     $this->name=$jemali; 
     $this->price=$zviadi; 
     $this->desc=$chuuch; 
    } 

    public function getInfo(){ 
     return "product name:".$this->name; 
    } 
} 

class Soda extends Product { 
    public $flavor="default flavor"; 

    function __construct($jemali, $zviadi, $chuuch, $lavor){ 

     parent::__construct($jemali, $zviadi, $chuuch); 
     $this->flavor=$lavor; 
    } 

    public function getInfo(){ 
     return "product name:".$this->name." flavor ".$this->flavor; 
    } 
} 

//$shirt = new Product("miriani", 10, "magari"); 
$soda = new Soda("jemala", 12, "chuchuka", "grape"); 
//echo $shirt->getInfo(); 
echo "<br />"; 
echo $soda->getInfo(); 
?> 

希望這段代碼可以幫助您