CHAPTER 5 ■ OBJECT TOOLS
The code checks whether the argument is a reference with isPassedByReference(), and finally looks
for the availability of a default value, which it then adds to the return string.
Using the Reflection API
With the basics of the Reflection API under your belt, you can now put the API to work.
Imagine that you’re creating a class that calls Module objects dynamically. That is, it can accept plug-
ins written by third parties that can be slotted into the application without the need for any hard coding.
To achieve this, you might define an execute() method in the Module interface or abstract base class,
forcing all child classes to define an implementation. You could allow the users of your system to list
Module classes in an external XML configuration file. Your system can use this information to aggregate a
number of Module objects before calling execute() on each one.
What happens, however, if each Module requires different information to do its job? In that case, 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. Given that foundation, it’s up to your code to ensure
that the correct setter method is called for the correct property name.
Here’s some groundwork for the Module interface and a couple of implementing classes:
class Person {
public $name;
function __construct( $name ) {
$this->name = $name;
}
}
interface Module {
function execute();
}
class FtpModule implements Module {
function setHost( $host ) {
print "FtpModule::setHost(): $host\n";
}
function setUser( $user ) {
print "FtpModule::setUser(): $user\n";
}
function execute() {
// do things
}
}
class PersonModule implements Module {
function setPerson( Person $person ) {
print "PersonModule::setPerson(): {$person->name}\n";
}
function execute() {
// do things
}
}