I’m just getting to grips with OOP PHP and while I have understood it ok, I’m having some trouble with the final keyword. Final, the book says, stops the subclass method, overriding the superclass method. Below I’ve tried it and my IDE is showing some errors and it doesn’t work. I have looked and looked but the book says this should envoke the superclass method not the subclass.
$object=new userprofile();
$object->name="Mike";
$object->age=22;
$object->sex="Male";
//2 properties of subclass
$object->email="username@domain.com";
$object->website="http://domain.com";
echo $object->get_name(); //method call
class user{
public $name, $age, $sex;
final function get_name(){
return "Not overriden";
}
}
class userprofile extends user{
public $email,$website;
function get_name(){
return $this->website;
}
}
As the error message says, you can’t define a method that would override a method that’s been tagged final. You aren’t even allowed to try (and why would you, because the new method wouldn’t have any effect).
See also the documentation for the final keyword which describes this in the first example.