-2

嘿,我对 oop 还很陌生,在学习的过程中,我遇到了一个折磨我超过 2 小时的问题。

你能告诉我为什么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 风味默认风味

4

1 回答 1

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();
?>

希望这可以帮助你

于 2015-02-08T16:51:38.983 回答