PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 26, 2022

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

 April 26, 2022     function, php, properties, variables, warnings     No comments   

Issue

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

<?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;
    }
}
.....

Edit: This problem is fixed. No need for new answers!


Solution

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

public Cat(){
  makeSound(sound);
}

public Dog(){
  makeSound(sound);
}


Answered By - sulox32
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing