Just in case you haven't had the chance to look at the preceding article of the series, where I discussed using the reflection API to get miscellaneous information about an example class, I'm going to include a brief view. Below I reintroduced the definition of this class and its associated interface, along with a script that pulls out mixed data from these two structures. Here's the source code of the interface and its implementing class: interface Identifier { public function setId($id);
public function getId();
}
/** * A sample user class * * @param id fname lname email */ class User implements Identifier { private $id = NULL; private $fname = 'Alejandro'; private $lname = 'Gervasio'; private $email = 'alejandro@domain.com'; const HEADING = 'Using the Reflection API in PHP 5';
// constructor (not implemented) public function __construct(){}
//setter for id property public function setId($id) { if (!is_numeric($id)) { throw new Exception('ID must be a numeric value'); } $this->id = $id; }
// getter for id property public function getId() { return $this->id; }
// setter for fname property public function setFirstName($fname) { if (empty($fname) OR !is_string($fname)) { throw new Exception('First name must be a non-empty string.'); } $this->fname = $fname; }
// getter for fname property public function getFirstName() { return $this->fname; }
// setter for lname property public function setLastName($lname) { if (empty($fname) OR !is_string($fname)) { throw new Exception('Last name must be a non-empty string.'); } $this->lname = $lname; }
// getter for lname property public function getLastName() { return $this->lname; }
// setter for email property public function setEmail($email) { if (empty($email) OR !is_string($email) OR strpos($email, '@') === FALSE) { throw new Exception('Email must be a well-formatted email address.'); } $this->email = $email; }
// getter for email property public function getEmail() { return $this->email; } } As you can see, the previous "User" class and the pertinent "Identifier" interface are nothing but simple examples for putting the reflection API to work. The following code fragment shows how to use the API to get general information about that class by using a few straightforward methods. Check it out: // create instance of 'User' class $user = new User();
// create instance of Reflection class and pass in 'User' class as argument $reflector = new ReflectionClass('User');
// get default properties in reflected class print_r($reflector->getDefaultProperties()); // displays Array ( [id] => [fname] => Alejandro [lname] => Gervasio [email] => alejandro@domain.com )
// get doc comments in reflected class print_r($reflector->getDocComment()); // displays /** * A sample user class * * @param id fname lname email */ // get starting line number of reflected class echo $reflector->getStartLine(); // displays '15'
// get ending line number of reflected class echo $reflector->getEndLine(); // displays '87'
// get filename of reflected class echo $reflector->getFileName(); // displays 'C:pathtoreflection_apireflection_api.php'
// get name of implemented interfaces print_r($reflector->getInterfaceNames()); // displays Array ( [0] => Identifier )
// get associative array of implemented interfaces print_r($reflector->getInterfaces()); // displays Array ( [0] => Identifier ) Array ( [Identifier] => ReflectionClass Object ( [name] => Identifier ) ) Retrieving miscellaneous data about the earlier "User" class is a simple process, thanks to the use of some intuitive reflection methods. In the above example, those methods are utilized to determine the starting and ending lines of the class, and the name of its containing file. As a bonus, the API is finally employed to get the names of the interfaces implemented by "User," which in this case is "Identifier," whose corresponding definition was shown before. Okay, now that you know how to use the power of reflection to analyze certain internal aspects of a class, I'm going to teach you how to get relevant information about the methods defined by the "User" class. This includes the ability to check whether these methods are public, protected or private. To learn the full details of this reflection process, click on the link below and read the section to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|