Page 230 - Beginning PHP 5.3
P. 230

Part II: Learning the Language
                    $testObject = new MyClass;
                    echo isset( $testObject- > banana ) . “ < br / > ”;      // Displays “” (false)
                    echo isset( $testObject- > testBanana ) . “ < br / > ”;  // Displays “1” (true)

                    __unset()  is called when the calling code attempts to delete an invisible property with PHP ’ s
                   unset()  function. It shouldn ’ t return a value, but should do whatever is necessary to  “ unset ”  the
                 property (if applicable):

                    class MyClass {

                      public function __unset( $propertyName ) {
                        echo “Unsetting property ‘$propertyName’ < br / > ”;
                      }
                    }

                    $testObject = new MyClass;

                    unset( $testObject- > banana );  // Displays “Unsetting property ‘banana’”
                    __callStatic()  works like  __call() , except that it is called whenever an attempt is made to call an
                 invisible static method. For example:
                    class MyClass {

                      public static function __callStatic( $methodName, $arguments ) {
                        echo “Static method ‘$methodName’ called with the arguments: < br / > ”;
                        foreach ( $arguments as $arg ) {
                          echo “$arg < br / > ”;
                        }
                      }
                    }


                    MyClass::randomMethod( “apple”, “peach”, “strawberry” );
                   This code produces the following output:
                    Static method ‘randomMethod’ called with the arguments:
                    apple
                    peach
                    strawberry



                  Using Inheritance to Extend the

              Power of Objects

                   So far, all the classes you ’ ve created have been self - contained. However, objects get really interesting
                 when you start using inheritance. Using this technique, you can create classes  —  known as  child
                classes  —  that are based on another class: the  parent class . A child class inherits all the properties and

                methods of its parent, and it can also add additional properties and methods.




              192





                                                                                                      9/21/09   9:03:40 AM
          c08.indd   192                                                                              9/21/09   9:03:40 AM
          c08.indd   192
   225   226   227   228   229   230   231   232   233   234   235