Overview
When you define a child class that extends a parent class, the child class inherits all properties and methods from its parent class.
You can add as many properties and methods as you want in your child class, but you can also override them to change their implementation (in the case of methods) or initial value (in the case of constants).
Overriding class methods
To override a parent method, redefine the method in the child class and use the same function signature: the same name, the same parameters, and the same return type.
Example
Activity
In the previous Animal / Bird example, what do you expect will be the output of $eagle->move()?
Calling the parent method from the child class
You can call the parent constructor from your child constructor using parent::__construct().
In fact, when you are overriding any parent method, you can still call the parent method from the child class using parent::name_of_the_method():
Output:
This animal is moving. Flap flap flap...
This animal is moving. Splash splash splash...This pattern is interesting to use if you want to extend a parent method without duplicating all of its code.
Overriding class constants
You can override constants as well as methods by redefining them in your child class:
Final: preventing overridden methods or constants
In certain situations you might want to completely prevent classes from being extended, or prevent child classes from overriding certain parent methods or constants.
You can use the final keyword to do so:
Example: final class
The following example defines an Animal class that is marked as final, and a Fish class that attempts to extend from Animal.
This code will result in an error.
Output:
Fatal error: Class Fish cannot extend final class Animal.Example: final method
In the following example, the Animal class itself is no longer final, but we've made the move() method final.
This code will also result in an ERROR, but a different one.
Output:
Fatal error: Cannot override final method Animal::move()Example: final const
Lastly, you can also make consts final. You would do this if you are absolutely certain that the const should never have any other value than the one you provide in your class definition.
The following code will result in an ERROR:
Output:
Fatal error: MyCircle::PI cannot override final constant Circle::PISummary
- Child classes can override their parent's methods and constants.
- From within a class, use
self::NAME_OF_CONSTto access a constant. - From outside a class, use
$myobject:NAME_OF_CONST: - If a child class overrides a parent's methods or constants, the parent's methods or constants can still be called directly via the
parent::syntax. - Use the
finalkeyword to:- completely prevent a class from being extended
- prevent a method from being overridden
- prevent a const from being overridden