Home arrow PHP arrow Page 3 - Getting More Info on Reflected Properties with the Reflection API

Checking for a class property's existence with the hasProperty() reflection method - PHP

In this fifth part of the series you will learn a couple of additional methods included with the PHP reflection API. The first one will allow you to check the existence of a specified class property, and the second one will permit you to retrieve all of the static properties defined by that class at once.

TABLE OF CONTENTS:
  1. Getting More Info on Reflected Properties with the Reflection API
  2. Review: getting basic information about class properties with reflection
  3. Checking for a class property's existence with the hasProperty() reflection method
  4. Retrieving all of the static properties of a class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
March 22, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: