Searching...
Monday 29 July 2013

Helper Functions in Object Oriented PHP


A number of functions are available to help the developer manage and use class libraries. These functions (more about functions in PHP)  are introduced in this section.

Creating a Class Alias

The class_alias() function creates a class alias, allowing the class (more about classes in PHP) to be referred to by more than one name. Its prototype follows:

boolean class_alias(string originalClassName, string aliasName)

This function was introduced in PHP 5.3.


Determining Whether a Class Exists

The class_exists() function returns TRUE if the class specified by class_name exists within the currently executing script context and returns FALSE otherwise. Its prototype follows:

boolean class_exists(string class_name)


Determining Object Context

The get_class() function returns the name of the class to which object (more about objects in PHP) belongs and returns FALSE if object is not an object. Its prototype follows:

string get_class(object object)


Learning about Class Methods

The get_class_methods() function returns an array containing all method names defined by the class class_name. Its prototype follows:

array get_class_methods(mixed class_name)

 

Learning about Class Properties

The get_class_vars() function returns an associative () containing the names of all properties and their corresponding values defined within the class specified by class_name. Its prototype follows:

array get_class_vars(string class_name)

 

Learning about Declared Classes

The function get_declared_classes() returns an array containing the names of all classes defined within the currently executing script. The output of this function will vary according to how your PHP distribution is configured (more about configuring PHP). For instance, executing get_declared_classes() on a test server produces a list of 97 classes. Its prototype follows:

array get_declared_classes(void)

 

Learning about Object Properties

The function get_object_vars() returns an associative array containing the defined properties available to object and their corresponding values. Those properties that don’t possess a value will be assigned NULL within the associative array. Its prototype follows:

array get_object_vars(object object)

 

Determining an Object’s Parent Class

The get_parent_class() function returns the name of the parent of the class to which object belongs. If object’s class is a base class, that class name will be returned. Its prototype follows:

string get_parent_class(mixed object)

 

Determining Interface Existence

The interface_exists() function determines whether an interface exists, returning TRUE if it does and FALSE otherwise. Its prototype follows:

boolean interface_exists(string interface_name [, boolean autoload])


Determining Object Type

The is_a() function returns TRUE if object belongs to a class of type class_name or if it belongs to a class that is a child of class_name. If object bears no relation to the class_name type, FALSE is returned. Its prototype follows:

boolean is_a(object object, string class_name)

Oddly, this function was temporarily deprecated from PHP 5.0.0 to PHP 5.3.0, causing an E_STRICT warning to be displayed during this time.

Determining Object Subclass Type

The is_subclass_of() function returns TRUE if object (which can be passed in as type string or object) belongs to a class inherited from class_name and returns FALSE otherwise. Its prototype follows:

boolean is_subclass_of(mixed object, string class_name)

 

Determining Method Existence

The method_exists() function returns TRUE if a method named method_name is available to object and returns FALSE otherwise. Its prototype follows:

boolean method_exists(object object, string method_name)


Autoloading Objects

For organizational reasons, it’s common practice to place each class in a separate file. Returning to the library scenario, suppose the management application calls for classes representing books, employees, events, and patrons. Tasked with this project, you might create a directory named classes and place the following files in it: Books.class.php,  mployees.class.php, Events.class.php, and Patrons.class.php. While this does indeed facilitate class management, it also requires that each separate file be made available to any script requiring it, typically through the require_once() statement. Therefore, a script requiring all four classes would require that the following statements be inserted at the beginning:

require_once("classes/Books.class.php");
require_once("classes/Employees.class.php");
require_once("classes/Events.class.php");
require_once("classes/Patrons.class.php");

Managing class inclusion in this manner can become rather tedious and adds an extra step to the already often complicated development process. To eliminate this additional task, the concept of auto loading objects was introduced in PHP 5. Autoloading allows you to define a special __autoload function that is automatically called whenever a class is referenced that hasn’t yet been defined in the script. You can eliminate the need to manually include each class file by defining the following function:


function __autoload($class) {
require_once("classes/$class.class.php");
}

Defining this function eliminates the need for the require_once() statements because when a class is invoked for the first time, __autoload() will be called, loading the class according to the commands defined in __autoload(). This function can be placed in a global application configuration file, meaning only that function will need to be made available to the script.

More components of Object Oriented PHP <-> Advanced Object Oriented PHP

0 comments:

Post a Comment

 
Back to top!