Inheritance of Private Methods with Parameter Contravariance
PHP 8 extends the support for contravariant parameters to private methods in subclasses. Previously, this was not allowed due to potential violations of type safety, but PHP 8 relaxes this restriction.
Example: This example shows the inheritance of Private Methods with Parameter Contravariance.
class ParentClass {
private function process(string $data): void {
echo "ParentClass processing: $data";
}
}
class ChildClass extends ParentClass {
private function process(int $data): void {
echo "ChildClass processing: $data";
}
}
// Usage
$child = new ChildClass();
$child->process(10);
Output:
ChildClass processing: 10
What is New in PHP Type Hinting Support in PHP 8?
PHP 8 introduced several enhancements and new features related to type hinting. That will check the code for their respective type so that we can use our code efficiently.
These are the following features used in PHP Type Hitting Support:
Table of Content
- Union Types
- Mixed Type
- Static Return Type
- Inheritance of Contravariant Parameters
- Inheritance of Private Methods with Parameter Contravariance