A finalized
method is declared like this:
class Employee
{
...
final function getName() {
...
}
}
Attempts to later override a finalized method result in a fatal error. PHP supports
six method scopes: public, private, protected, abstract, final, and static.
?– Note The topics of class inheritance and the overriding of methods and fields are discussed in the
next chapter.
Type Hinting
Type hinting is a feature introduced with the PHP 5 release. Type hinting ensures that
the object being passed to the method is indeed a member of the expected class. For
example, it makes sense that only objects of class Employee should be passed to the
takeLunchbreak() method. Therefore, you can preface the method definition??™s sole
input parameter $employee with Employee, enforcing this rule. An example follows:
182 CHAPTER 6 ?– O BJECT-ORIENTED PHP
private function takeLunchbreak(Employee $employee)
{
...
}
Keep in mind that type hinting only works for objects and arrays. You can??™t offer
hints for types such as integers, floats, or strings.
Constructors and Destructors
Often, you??™ll want to execute a number of tasks when creating and destroying objects.
For example, you might want to immediately assign several fields of a newly instantiated
object. However, if you have to do so manually, you??™ll almost certainly forget to
execute all of the required tasks. Object-oriented programming goes a long way toward
removing the possibility for such errors by offering special methods, called constructors
and destructors, that automate the object creation and destruction processes.
Pages:
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261