CodingBison

PHP provides several functions that interact with objects and classes. This page covers some of the commonly used object-related functions. For the sake of convenience, we organize these functions into two categories.

The first category covers validation functions; we provide the list below. These functions allow us to examine if a given element (class, object, method, or property) exists; they return true if the referenced element (class, method, or property) exists; else, they return false.

We provide below their signature:

 $boolean_exists = class_exists(className);
 $boolean_exists = method_exists(className, methodName);
 $boolean_exists = method_exists($object, methodName);
 $boolean_exists = property_exists(className, methodName);
 $boolean_exists = property_exists($object, propertyName);

The method class_exists() returns true if a given class exists. Functions method_exists() and property_exists() accept both class name and object name. It is important to note that we do not need to provide the "$" prefix when passing class names, but still need to provide the "$" prefix when passing object names.

The second category covers functions that retrieve various information about classes and objects; we provide the list below.

 $name                     = get_class($object);
 $list_of_class_variables  = get_class_vars(className);
 $list_of_class_methods    = get_class_methods(className);
 $list_of_object_variables = get_object_vars($object);

The function get_class() returns the class name for an object. Functions get_class_vars() and get_class_methods() return a list of properties and methods of the class respectively.

Function get_object_vars() returns object variables along with their values. This function prints value of these variables because unlike a class, an object holds variables and their values. This list also includes those properties that are added on the fly. Since we cannot add methods dynamically to an object, having get_class_methods() is sufficient and perhaps, this is the reason why PHP does not provide get_object_methods() function!

Let us now demonstrate the usage of these functions using a simple PHP program (provided below). The program illustrates functions from both of the above categories. Further, to highlight the ability of get_object_vars() to print even dynamically added variables, we dynamically assign a variable, "zip" to the "$me_as_user" object.

 <?php
 class BasicUser {
     var $userID, $name, $age, $city;

     function __construct($varID, $varName, $varAge) {
         $className = get_class($this);
         echo "Calling Constructor() function for $className class <br>";
         $this->userID = $varID;
         $this->name = $varName;
         $this->age = $varAge;
     }   

     function set_city($varCity) {
         $this->city = $varCity;
     }   

     function print_userinfo() {
         echo "<br>Printing User Details: <br>";
         echo "Userid: $this->userID<br>";
         echo "Name: $this->name<br>";
         echo "Age: $this->age<br>";
         echo "City: $this->city<br>";
     }   
 }

 if (class_exists('BasicUser')) {
     $me_as_user = new BasicUser("ksimon", "Karuna Simon", 32);
     $me_as_user->print_userinfo();

     if (method_exists($me_as_user,'set_city')) {
         $me_as_user->set_city("Chicago");
     }
     $me_as_user->print_userinfo();

     if (property_exists($me_as_user, 'city')) {
         $me_as_user->city = "New York";
     }
     $me_as_user->print_userinfo();

     $class_vars = get_class_vars(get_class($me_as_user));
     echo "<br>Printing ClassVariables:<br>";
     print_r($class_vars);

     $me_as_user->zip = 23232;
     echo "<br><br>Printing Object Variables:<br>";
     $object_vars = get_object_vars($me_as_user);
     print_r($object_vars);

     echo "<br><br>Printing Class Methods:<br>";
     $class_methods = get_class_methods(get_class($me_as_user));
     print_r($class_methods);
 }
 ?>

The output (provided below) confirms the differences between get_class_vars() and get_object_vars(). When we print the list of object variables using get_object_vars(), it also prints dynamically added variables like "zip". Since "zip" does not exist as a variable of the original class (BasicUser), get_class_vars() does not print this variable.

 Calling Constructor() function for BasicUser class 

 Printing User Details: 
 Userid: ksimon
 Name: Karuna Simon
 Age: 32
 City: 

 Printing User Details: 
 Userid: ksimon
 Name: Karuna Simon
 Age: 32
 City: Chicago

 Printing User Details: 
 Userid: ksimon
 Name: Karuna Simon
 Age: 32
 City: New York

 Printing ClassVariables:
 Array ( [userID] => [name] => [age] => [city] => ) 

 Printing Object Variables:
 Array ( [userID] => ksimon [name] => Karuna Simon [age] => 32 [city] => New York [zip] => 23232 ) 

 Printing Class Methods:
 Array ( [0] => __construct [1] => set_city [2] => print_userinfo )




comments powered by Disqus