skip to Main Content

I am getting the error "Undefined property ‘$title’. intelephense (1014)" for the employee class.


class User {
    // Properties are attributes that belong to a class
    public $name;
    public $email;
    public $password;

    public function __construct($name, $email, $password) {
        $this->name = $name;
        $this->email = $email;
        $this->password = $password;
    }

    function set_name($name) {
        $this->name = $name;
    }

    function get_name() {
        return $this->name;
    }
}

// Inheritence
class Employee extends User {
    public function __construct($name, $email, $password, $title)
    {
        parent::__construct($name, $email, $password);
        $this->title = $title;
    }
    public function get_title() {
        return $this->title;
    }
}

$employee1 = new Employee('Sara', '[email protected]', '123', 'manager');
echo $employee1->get_title;

I am also getting the same error for ‘$get_title’ when I try to echo on the last line.

I was expecting to see the employee’s title: ‘manager’.

3

Answers


  1. It’s not :

    echo $employee1->get_title;
    

    but :

    echo $employee1->get_title();
    

    Try using an IDE to develop, this will avoid this kind of error thanks to autocompletion. VSCode for example.

    Edit: you forgot the declaration of $title in the class Employees

    Login or Signup to reply.
  2. Go to VS Code Settings (Ctrl + ,) and search for "intelephens.diagnostics.undefined":

    Find and uncheck frm User and Workspace: Enables undefined static property diagnostics. It should work now! enter image description here

    Login or Signup to reply.
  3. You forgot to declare public $title; in class User

    class User {
        public $name;
        public $email;
        public $password;
        public $title;
    }
    

    the error will be gone

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search