As always, before I start explaining how to use the functionality of reflection to check if a particular class is native or user-defined, I'm going to re-list the examples developed in the previous article. They demonstrated how useful the reflection API can be for finding out whether a class is abstract, is final, and if it's an implementer of a given interface. Having explained that, please focus your attention on the following code samples, which show how to perform the aforementioned reflective tasks in a really simple fashion. First, here's the sample interface and its implementing class used to test the pertinent reflection methods: 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; } } Now that I have listed the definitions corresponding to the "Identifier" interface and its implementing "User" class, let's look at the example below. It shows how to use a few reflection methods to determine whether that class is abstract or final, and if it actually is an implementer of that interface: // 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 is abstract if ($reflector->isAbstract()) { echo 'Reflected class is abstract'; // displays 'Reflected class is not abstract' } else { echo 'Reflected class is not abstract'; }
// check if the reflected class is final if ($reflector->isFinal()) { echo 'Reflected class is final'; // displays 'Reflected class is not final' } else { echo 'Reflected class is not final'; }
// check if the reflected class is an implementer of a specified interface if ($reflector->implementsInterface('Identifier')) { echo 'Reflected class is an implementer of the specified interface'; // displays 'Reflected class is an implementer of the specified interface' } else { echo 'class is not an implementer of the specified interface'; }
// check if the reflected class can be instantiated if ($reflector->isInstantiable()) { echo 'Reflected class is instantiable'; // displays 'Reflected class is instantiable' } else { echo 'Reflected class is not instantiable'; } From the previous example, it's clear to see how simple it is to know if a given class can be instantiated or not via the "isAbstract()" and "isInstantiable()" methods respectively. As a bonus, the "implementsInterface()" method will check to see if the reflected class is an implementer of the specified interface, which in this case happens to be true. So far, so good. You've now mastered a few additional methods packaged with the reflection API that allowed you to obtain detailed information about a class. So it's time to discuss some other reflective methods that will fit your expectations. In keeping with this idea, in the following segment I'm going to show how to use the brand new "isInternal()" method for checking whether or not a class is native. To learn more about how to use this reflective method, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|