Читать книгу PHP This! A Beginners Guide to Learning Object Oriented PHP - Michelle Gosney - Страница 16

Method Overriding

Оглавление

Method overriding is where a child class (one derived through inheritance) defines a function with the same name and parameters as a function in its parent class, but provides a different implementation of the function. This is not the same as polymorphism, as it does not necessarily involve late-binding, however overriding is part of the mechanism by which polymorphism is accomplished.

All this can sound confusing at first but a simple code example can help clarify:


This is an example of overriding. It is different from polymorphism because we are declaring a Cow and directly calling a Cow function through Cow’s interface. However, PHP is a loosely-typed language, meaning that the vast majority of data type handling is done implicitly by PHP, rather than the programmer. This means the difference between overriding and polymorphism in PHP can appear rather subtle. Consider a slightly different example:


PHP This! A Beginners Guide to Learning Object Oriented  PHP

Подняться наверх