PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 7.11 Finding the Methods and Properties of an Object

7.11.1 Problem

You want to inspect an object to see what methods and properties it has, which lets you write code that works on any generic object, regardless of type.

7.11.2 Solution

Use get_class_methods( ) and get_class_vars( ) to probe an object for information:

// learn about cars
$car_methods = get_class_methods('car');
$car_vars    = get_class_vars('car');

// act on our knowledge
if (in_array('speed_away', $car_methods)) {
    $getaway_van = new car;
    $getaway_van->speed_away( );
}

7.11.3 Discussion

It's rare to have an object and be unable to examine the actual code to see how it's described. Still, these functions can be useful for projects you want to apply to a whole range of different classes, such as creating automated class documentation, generic object debuggers, and state savers, like serialize( ).

Both get_class_methods( ) and get_class_vars( ) return an array of values. In get_class_methods( ), the keys are numbers, and the values are the method names. For get_class_vars( ), both variable names and default values (assigned using var) are returned, with the variable name as the key and the default value, if any, as the value.

Another useful function is get_object_vars( ) . Unlike its sister function get_class_vars( ), get_object_vars( ) returns variable information about a specific instance of an object, instead of a generic newly created object.

As a result, you can use it to check the status of an object as it currently exists in a program:

$clunker = new car;
$clunker_vars = get_object_vars($clunker); // we pass the object, not the class

Since you want information about a specific object, you pass the object and not its class name. But, get_object_vars( ) returns information in the same format as get_class_vars( ).

This makes it easy to write quick scripts to see if you're adding new class variables:

$new_vars = array_diff(array_keys(get_object_vars($clunker)),
                       array_keys(get_class_vars('car')));

You extract the variable names using array_keys( ) . Then, with the help of array_diff( ), you find which variables are in the $clunker object that aren't defined in the car class.

If you just need a quick view at an object instance, and don't want to fiddle with get_class_vars( ), use either var_dump( ) , var_export( ), or print_r( ) to print the object's values. Each of these three functions prints out information in a slightly different way; var_export( ) can optionally return the information, instead of displaying it.

7.11.4 See Also

Recipe 5.9 for more on printing variables; documentation on get_class_vars( ) at http://www.php.net/get-class-vars, get_class_methods( ) at http://www.php.net/get-class-methods, get_object_vars( ) at http://www.php.net/get-object-vars, var_dump( ) at http://www.php.net/var-dump, var_export( ) at http://www.php.net/var-export, and print_r( ) at http://www.php.net/print-r.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    7.1 Introduction
    Recipe 7.2 Instantiating Objects
    Recipe 7.3 Defining Object Constructors
    Recipe 7.4 Destroying an Object
    Recipe 7.5 Cloning Objects
    Recipe 7.6 Assigning Object References
    Recipe 7.7 Calling Methods on an Object Returned by Another Method
    Recipe 7.8 Accessing Overridden Methods
    Recipe 7.9 Using Property Overloading
    Recipe 7.10 Using Method Polymorphism
    Recipe 7.11 Finding the Methods and Properties of an Object
    Recipe 7.12 Adding Properties to a Base Object
    Recipe 7.13 Creating a Class Dynamically
    Recipe 7.14 Instantiating an Object Dynamically
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele