Page 221 - Beginning PHP 5.3
P. 221

Chapter 8: Objects

                               public function getTotalBalance() {
                                 return $this- > _totalBalance;
                               }
                             }

                             $a = new Account;
                             $a- > makeDeposit( 500 );
                             $a- > makeWithdrawal( 100 );
                             echo $a- > getTotalBalance() . “ < br / > ”;  // Displays “400”;

                             $a- > makeWithdrawal( 1000 );  // Displays “Insufficient funds”
                           Because the variable storing the account balance is private, it can ’ t be manipulated directly. Customers
                         have to actually make a deposit via   makeDeposit()  if they want to increase the value of their account.
                            By encapsulating internal data and method implementations, an object - oriented application can protect
                          and control access to its data and hide the details of implementation, making the application more
                          flexible and more stable.



                           Object Overloading with                      get(),
                                                                    —
                          set(), and              call()
                       —                      —
                           Normally, if you try to read or write an object ’ s property, PHP dutifully reads or sets the property ’ s value
                         (assuming the property exists and your code has permission to access it). Similarly, if you call an object ’ s
                         method, PHP looks for the method within the object and, if it finds it, runs it.
                           However, PHP lets you use a technique known as  overloading  to intercept attempts to read or write an
                         object ’ s properties, or call its methods. This can be quite powerful. As far as the calling code is
                         concerned, the object contains fixed, pre - programmed properties and methods. However, behind the
                         scenes, your object can be doing all sorts of interesting things. For example:
                            ❑       The calling code reads the value of   $myObject - > property , which actually causes  $myObject

                                to retrieve the value from an array instead
                            ❑       The calling code sets   $myObject - > anotherProperty  to a new value, but behind the scenes

                                  $myObject  actually writes this value to a database field
                            ❑       The calling code calls   $myObject - > aMethod() . This method doesn ’ t actually exist in

                                  $myObject , but  $myObject  intercepts the call and calls another method instead
                           Although you probably won ’ t use object overloading that often, you can see that the technique can offer
                         you a lot of flexibility.











                                                                                                         183





                                                                                                      9/21/09   9:03:37 AM
          c08.indd   183
          c08.indd   183                                                                              9/21/09   9:03:37 AM
   216   217   218   219   220   221   222   223   224   225   226