Among the plethora of methods that come with the reflection API, there’s one called “hasProperty().” As its name suggests, it can be used for verifying the existence of a specified property within a class. This method takes the name of the property to be inspected as an incoming argument, and an example of its usage is shown below: // create instance of 'User' class $user = new User();
// create instance of Reflection class and pass in 'User' class as 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'; } As you can see in the above code fragment, the “hasProperty()” method can be really helpful for checking the existence of a given class property. In this case, the property being checked within the “User” class is “lname,” but of course it’s possible to inspect others with the same ease. Now that you've surely learned how to utilize one method of the reflection API to know if a class declares a specified property, it’s time to explore a few others. In consonance with this premise, in the last segment of this tutorial I’m going to explain how to get all of the static properties defined by the sample “User” class. Want to see how this will be done? Then click on the link below and read the following section.
blog comments powered by Disqus |
|
|
|
|
|
|
|