In the previous article, as I mentioned, I explained how to use the “hasProperty()” and “getStaticProperties()” reflection methods with a sample class. Below I reintroduced the source code corresponding to these examples, starting with the definitions of the class itself and its associated interface. Here they are: 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; } } Since the “Identifier” interface and its implementing “User” class have been coded only for demonstration purposes, explaining how they function (again) would be a waste of time. Instead, you should look at the following code fragment, which shows how to use the aforementioned “hasProperty()” and “getStaticProperties()” methods, first for checking the existence of a specific property, and then for getting all of the static properties defined by this class: // create instance of 'User' class $user = new User();
// create instance of Reflection class and pass in 'User' class as an argument
$reflector = new ReflectionClass('User');
// check if the reflected class has a specified property if ($reflector->hasProperty('lname')) { echo 'Property found in reflected class'; // displays 'Property found in reflected class' } else { echo 'Property not found in reflected class'; }
// get static properties of reflected class print_r($reflector->getStaticProperties()); // displays nothing Undeniably, getting information about the properties of a class is a no-brainer process, thanks to the numerous methods included with the reflection API. Again, it’s fair to stress here that the API offers many other handy methods that will let you analyze your classes and interfaces down to their bare bones, so if you need to perform a particular introspection task that hasn’t been covered in this series, make sure to check the API’s official online documentation. So far, so good. At this stage, you've learned how to obtain relevant data about the properties of a class, so now I’m going to discuss using reflection to know if a class has been declared abstract and final. These subjects will be covered in the section to come. To get there, simply click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|