Page 229 - Beginning PHP 5.3
P. 229

Chapter 8: Objects
                             echo “ < p > The letter ‘e’ occurs at position: “ . $myString- > strpos( “e” ) .
                             “ < /p > ”;
                             $myString- > madeUpMethod();

                           The first two method calls,  $myString - > strlen()  and  $myString - > strtoupper() , don ’ t have any


                         arguments because their equivalent PHP functions only require one argument  —  the string to work
                         with  —  and this is automatically populated with the stored string thanks to the   __call()  method.
                         The third method call,   $myString - > strpos(  “ e ”  ) , requires a single argument  —  the string to

                         search for  —  which is then passed as the second argument to PHP ’ s   strpos()  function.
                           The following table shows how the   CleverString  method calls map to the actual PHP
                         string functions:


                                     M ethod  C all                 PHP S tring  F unction  C all

                                       $myString - > strlen()         strlen( $this - > _theString )


                                       $myString - > strtoupper()        strtoupper( $this - > _theString )


                                       $myString - > strpos(  “ e ”  )        strpos( $this - > _theString,  “ e ”  )


                           Finally, the script attempts to call a disallowed  —  in fact, nonexistent  —  string function, which
                         displays an error message in the page:

                             $myString-> madeUpMethod();

                           This example shows how easy it is to wrap a set of existing functions, methods, or API calls in a class
                         using a single   __call()  method. You could easily extend this example to allow practically all of
                         PHP ’ s tens of string functions to be called, without having to write much extra code.





                           Other Overloading Methods

                           Although you probably won ’ t use them much, it ’ s worth mentioning three other overloading methods
                         provided by PHP:

                            __isset()  is called whenever the calling code attempts to call PHP ’ s  isset()  function on an invisible
                         property. It takes one argument  —  the property name  —  and should return   true  if the property is
                          deemed to be  “ set, ”  and   false  otherwise:
                             class MyClass {

                               public function __isset( $propertyName ) {
                                 // All properties beginning with “test” are “set”
                                 return ( substr( $propertyName, 0, 4 ) == “test” ) ? true : false;
                               }
                             }



                                                                                                         191





                                                                                                      9/21/09   9:03:39 AM
          c08.indd   191
          c08.indd   191                                                                              9/21/09   9:03:39 AM
   224   225   226   227   228   229   230   231   232   233   234