Page 253 - Beginning PHP 5.3
P. 253

Chapter 8: Objects
                               In a real - world situation, make sure you don ’ t transmit sensitive information such as usernames as
                             passwords as plain text strings if there ’ s a chance that the data might be intercepted or read by
                             untrusted third parties.
                            If you do need to preserve all your object ’ s properties, you can use the built - in   get_object_vars()
                         function to get an associative array of all the properties in the object, then use the   array_keys()  function
                         to get just the property names as an array, which you can then return from your   __sleep()  method:

                             class User {
                               public $username;
                               public $password;
                               public $loginsToday;

                               public function __sleep() {
                                 // (Clean up; close database handles, etc)
                                 return array_keys( get_object_vars( $this ) );

                               }
                             }

                           Finally, here ’ s an example that shows the  __wakeup()  method in action:
                             class User {
                               public function __wakeup() {
                                 echo “Yawn... what’s for breakfast? < br / > ”;
                               }
                             }

                             $user = new User;
                             $userString = serialize( $user );
                             $obj = unserialize( $userString );  // Displays “Yawn... what’s for breakfast?”



                           Determining an Object ’ s Class

                           Earlier in the chapter you learned that you can use hints in method and function arguments to ensure
                         that the correct class of object is being passed. Sometimes, though, you might want to explicitly
                         check the class of a particular object that you ’ re working with. For example, you might want to check
                         that all the objects in an array are of a certain class, or treat objects differently depending on their class.

                           To find out the class of an object, you can use PHP ’ s built - in   get_class()  function, as follows:

                             class MyClass {
                             }

                             $obj = new MyClass();

                             echo get_class( $obj ); // Displays “MyClass”






                                                                                                         215





          c08.indd   215
          c08.indd   215                                                                              9/21/09   9:03:48 AM
                                                                                                      9/21/09   9:03:48 AM
   248   249   250   251   252   253   254   255   256   257   258