如何解决注意:PHP中的未定义变量错误

时间:2020-09-30 18:15:53

标签: php undefined undefined-variable

我目前正在用php创建一个名为Pizza的类,它具有两个属性:名称和价格。


    <?php
    
    class Pizza{
        private $name;
        private $price;
    
        public function __construct($name, $price){
            $this->setName($name);
            $this->setPrice($price);
        }
    
        public function getName(){
            return $this->$name;
        }
    
        public function getPrice(){
            return $this->$price;
        }
    
        public function setName($name){
            $this->$name = $name;
        }
    
        public function setPrice($price){
            $this->$price = $price;
        }
    }
    
    $pizza1 = new Pizza('pepperoni', 10);
    $pizza1->getPrice();

但是当我为价格调用getter方法时,我仍然遇到这些错误:

( ! ) Notice: Undefined variable: price in C:\wamp\www\pw\crazy pizza\pizza.php on line 17
Call Stack
#   Time    Memory  Function    Location
1   0.0020  391472  {main}( )   ...\pizza.php:0
2   0.0020  391856  Pizza->getPrice( )  ...\pizza.php:30

( ! ) Notice: Undefined property: Pizza::$ in C:\wamp\www\pw\crazy pizza\pizza.php on line 17
Call Stack
#   Time    Memory  Function    Location
1   0.0020  391472  {main}( )   ...\pizza.php:0
2   0.0020  391856  Pizza->getPrice( )  ...\pizza.php:30

请问您知道我的代码有什么问题吗?

0 个答案:

没有答案