How to understand the problems related to PHP7.2 ignoring parent methods and the principle of Liskov substitution
This article mainly introduces "how to understand PHP7.2 ignore parent method and Liskov replacement principle related problems", in daily operation, I believe many people in how to understand PHP7.2 ignore parent method and Liskov replacement principle related problems have doubts, small make up consult all kinds of information, sort out simple and easy to use operation method, hope to answer "how to understand PHP7.2 ignore parent method and Liskov replacement principle related problems" doubts helpful! Next, please follow the small series to learn together!
PHP 7.2 subclass coverage method omitting parameter type features and Liskov substitution principle
PHP 7.2 has been out for a while, and as for what new improvements are available in the new version, as long as you care about PHP development, you should have read them. Here is just one new feature that can be misunderstood.
PHP 7.2 allows you to ignore the type hint of the parameter defined by the parent class method when a subclass overrides the parent class method:
class Foo{ public function bar(SomeClass $obj) {}}class Foobar extends Foo{ public function bar($obj) {} //This was an error before PHP 7.2}
I see some websites introduce this feature, saying that its purpose is to facilitate refactoring. If the parameter type of the parent class method changes later, the child class does not have to change it all again." Sounds like a good idea. Implicit in this statement is that if a subclass ignores the parameter type of a parent class method, it will still check the parameter type when called. The actual situation is not like this. Do an experiment and you will know: