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
It’s not :
but :
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
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!
You forgot to declare public $title; in class User
the error will be gone