PHP 5 Advanced OOP and Design Patterns

When you finish reading this chapter, you will have learned ? Overloading capabilities that can be controlled from PHP code ? Using design patterns with PHP 5 ? The new reflection … 85 CHAPTER 4 PHP 5 Advanced OOP and Design Patterns”I made up the term ‘object-oriented,’ and I can tell you I didn’t have C++ in mind.”—Alan Kay, OOPSLA ’97 4.1 I NTRODUCTION In this chapter, you learn how to use PHP’s more advanced object-oriented capabilities. When you finish reading this chapter, you will have learned? Overloading …
4.1 I NTRODUCTION In this chapter, you learn how to use PHP’s more advanced object-oriented capabilities. When you finish reading this chapter, you will have learned ? Overloading capabilities that can be controlled from PHP code ? Using design patterns with PHP 5 ? The new reflection API 4.2O VERLOADING C APABILITIES In PHP 5, extensions written in C can overload almost every aspect of the object syntax. It also allows PHP code to overload a limited subset that is most often needed. This section covers the overloading abilities that you can control from your PHP code. 4.2.1Property and Method Overloading PHP allows overloading of property access and method calls by implementing special proxy methods that are invoked if the relevant property or method doesn’t exist. This gives you a lot of flexibility in intercepting these actions and defining your own functionality. You may implement the following method prototypes: function __get($property) function __set($property, $value) function __call($method, $args)…. 4.2.2Overloading the Array Access Syntax It is common to have key/value mappings or, in other words, lookup dictionaries in your application framework. For this purpose, PHP supports associative arrays that map either integer or string values to any other PHP value. This feature was covered in Chapter 2, “PHP 5 Basic Language,” and in case you forgot about it, here’s an example that looks up the user John’s social- security number using an associative array which holds this information: print “John’s ID number is ” . $userMap["John"]; Associative arrays are extremely convenient when you have all the information at hand. But consider a government office that has millions of people in its database; it just wouldn’t make sense to load the entire database into the $userMap associative array just to look up one user. A possible alternative is to write a method that will look up the user’s id number via a database call. The previous code would look something like the following: print “John’s ID number is ” . $db->FindIDNumber(“John”); This example would work well, but many developers prefer the associative array syntax to access key/value-like dictionaries. For this purpose, PHP 5 enables you to overload an object so that it can behave like an array. Basically, it would enable you to use the array syntax, but behind the scenes, a method written by you would be called, which would execute the relevant database call, returning the wanted value. It is really a matter of personal preference as to what method to use. Sometimes, it is nicer to use this overloading ability than the verbosity of calling a method, and it’s up to you to decide which method suits you best. To allow your class to overload the array syntax, it needs to implement the ArrayAccess interface (see Figure 4.1).
Download PHP 5 Advanced OOP and Design Patterns.Pdf