PHP OOP - Object tools
This note is taken when reading Chapter5 - “PHP Objects, Patterns, and Practice - 2nd”. The purpose is for Quick Reference.
Looking for class
boolean class_exists($classname);
$myObj= new $classname();
boolean file_exists($filename);
String[] get_declared_classes();
Learning about an object or class
String get_class($object);
boolean ($object instanceof Class_Name) => $object is belong to Class_Name family
Learning about Methods
String[] get_class_methods(‘Class_Name’[or $object]);
for checking: boolean in_array($methodName, get_class_methods($object))
boolean is_callable(array($object, String $methodName);
boolean method_exists($object, String $methodName); => this $methodName may not callable
Learning about Properties
Array[‘varName’ => value] get_class_vars(‘Class_Name’);
Learning about Inheristance
String[or false] get_parent_class(‘Class_Name’[or $object]);
boolean is_subclass_of($object, ‘Class_Name’);
Method Invocation
- Using String to invoke a method: $object->$methodName();
- $returnVal= call_user_func(‘functionName’);
- $returnVal= call_user_func(array($object, $methodName), 20);
<=> $returnVal= $object->$methodName(20);
- $returnVal= call_user_func_array(array($object, $methodName), $arrayOfArguments); => this is useful in __call($method, $args) interceptor.
The PHP5’s Reflection API consists of built-in classes for analyzing properties, methods, and classes. It’s also designed to enhance the ability to work with PHP’s OO features, such as access control, interfaces and abstract classes.
$prod_class = new ReflectionClass(‘CD_Product’);
Reflection::export($prod_class); => this method will return a detail information of class(declaration of class, properties, method)
The var_dump($object) function is general-purpose tool for summarizing data.
Examining a Class: $prod_class->getName(); getFileName() =>full absoluted path; getStartLine(); getEndLine(); isInternal(); isInterface(); isAbstract(); isFinal(); isInstatiable(); isUserDefined();
Examining a Method:
/*Array of ReflectionMethod*/ $aMethods= $prod_class->getMethod();
foreach($aMethod as $reflMethod)
{
/*ReflectionMethod*/ $reflMethod->getName();…; isInternal(); isAbstract(); isPublic(); …; isStatic(); isFinal(); isConstructor(); returnReference() /*not a value, such as a object reference*/
}
Examining Method Arguments: ReflectionParameter
$reflMethod= $prod_class->getMethod(‘__construct’);
$reflParams= $reflMethod->getParameters();
foreach($reflParams as $reflParam)
{
$reflParam->getName(); getClass(); isPassedByReference();
}
—————-
The Reflection API is very useful when you want to create a class that calls Module objects dynamically. It can accept plug-ins written by third-parties that can be slotted into the application without the need for any hard coding. (Example in pages 91 in this book)
- define an execute() method in the Module interface or abstract base class
- forcing all child classes to define an implementation.
- allow the users of your system to list Module classes in an external XML configuration file.
- use this information to aggregate a number of Module objects before calling execute() on each one.
- the XML file can provide property keys and values for each Module, and the creator of each Module can provide setter methods for each property name
- your code ensures that the correct setter method is called for the correct property name