As usual, before I proceed to demonstrate how to use other useful methods included with the PHP reflection API, it’d be convenient to take a quick look at the code samples developed in the previous article. In that part of the series I showed how to get the name and the values assigned to the constant declared by a sample class. The class in question was a really basic one, and implemented an interface called “Identifier.” The definitions of this interface and the implementing class are shown below: 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; } } From the above code fragment, it’s clear to see that the purpose of coding the “Identifier” interface and its implementing “User” class is only to have two sample structures that can be easily analyzed internally via reflection. Things start to look more interesting, though, when these example entities are reverse-engineered through some reflective methods. The following code snippet shows how to get the name of a reflected class and information about the constants that it declares in a few simple steps: // 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 name of reflected class echo $reflector->getName(); // displays 'User' // get constant in reflected class echo $reflector->getConstant('HEADING'); // displays 'Using the Reflection API in PHP 5' // get an array of constants in reflected class print_r($reflector->getConstants()); // displays Array ( [HEADING] => Using the Reflection API in PHP 5 ) See how easy it is to collect information on a selected class via the reflection API? I guess you do! In this case, the information retrieved is somewhat trivial, but it demonstrates how powerful the API can be for stripping classes and interfaces down to their bare bones by means of an object-oriented approach. Of course, if you’re like me, you'll typically want to know more about a class or interface, aside from retrieving its name and its declared constants. Fortunately, the reflection API provides some other methods that allow you to fetch mixed class information in a simple manner. In the following section I’m going to teach you how to retrieve the set of default properties declared by the earlier “User” class, along with the data embedded into its doc block. To learn more about how this will be done, click on the link below and read the segment to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|