Page 227 - Beginning PHP 5.3
P. 227

Chapter 8: Objects

                             echo “ < p > The string in uppercase letters is: “ . $myString- > strtoupper() .
                             “ < /p > ”;
                             echo “ < p > The letter ‘e’ occurs at position: “ . $myString- > strpos( “e” ) .
                             “ < /p > ”;
                             $myString- > madeUpMethod();

                             ? >

                                < /body >
                               < /html >
                           When run, the script produces the output shown in Figure  8 - 4 .
























                                      Figure 8 - 4




                             H ow  I t  W orks
                          The  CleverString  class serves two purposes: it stores a string to be operated on, and it provides
                         method - based access to three built - in string functions that operate on the stored string:
                            ❑       strlen()  for calculating the length of the string
                            ❑       strtoupper()  for converting the string to uppercase letters
                            ❑       strpos()  for finding the position of the first occurrence of a character in the string
                           As mentioned earlier, it ’ s good practice to encapsulate the members of a class as much as possible in
                         order to make the class robust and maintainable. To this end, the stored string is encapsulated in a
                         private property,   $_theString ; calling code can use the public methods  setString()  and
                           getString()  to set and read the string value.








                                                                                                         189





                                                                                                      9/21/09   9:03:39 AM
          c08.indd   189
          c08.indd   189                                                                              9/21/09   9:03:39 AM
   222   223   224   225   226   227   228   229   230   231   232