2015-02-08 71 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(); 
?> 

希望这段代码可以帮助您