How to solve Notice: Undefined variable error in php

26 views Asked by At

I am currently creating a class named pizza in php with two attributes: name and price.


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

But when i am calling the getter method for the price, i keep having these error:

( ! ) 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

Please do you know what's wrong with my code?

0

There are 0 answers