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

Late Binding / Dynamic Binding

Оглавление

Late-binding (also known as dynamic binding) is the mechanism by which polymorphism is accomplished. In the example above, the speak() function is defined in the base class, but makes a call to getName(), a polymorphic function that has a specific implementation in each child class. Late-binding means that it is only at runtime, when the function is called, that the specific data type is bound to the request. In simple terms, even though speak() is defined only in the generic Animal class, it will call the child-specific implementation of getName(). This late-binding is a relatively new feature of PHP and is what makes true polymorphism possible.


This example is polymorphism, because the Name() function knows it is receiving an object of type Animal, but not whether it is a Cow, Dog or whatever else. Notice how we have specified type Animal as a parameter for the Name() function – specifying the data type of function parameters is normal and required in strongly-typed languages, but this type hinting is optional in PHP. Specifying a type hint will cause PHP to generate a fatal error if you attempt to pass the function a non-object, or object of a different type.

PHP is a dynamic programming language because it executes many common behaviors at run time that other languages might perform at compile time. It is loosely typed, or dynamically typed, in that a majority of the type checking is performed at run-time as opposed to being done at compile time. In dynamic typing values have types but variables do not, that it, a variable can refer to any value of any type.

PHP This! A Beginners Guide to Learning Object Oriented  PHP

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