[SOLVED] How to solve undefined variable and undefined property warnings?


I have $name and $age defined in set_name() and set_age.
Please check index.php:

// This part needs some fixing.
class Pet {
    public $name;
    public $age;    

    // get() and set() functions for name
    function set_name($name) {
        $this->name = $name;
    function get_name() {
        return $this->$name;

    // get() and set() functions for age
    function set_age($age) {
        $this->age = $age;
    function get_age() {
        return $this->$name;

This problem is fixed. No need for new answers!


You need constructor for your classes and don’t do the makeSound method in your Pet class static. Try this:

public Cat(){

public Dog(){

Answered By – sulox32

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *