As I said in the section that you just read, the reflection API makes it really easy to get all of the public, protected and private methods of a class. For the sake of brevity, in this case I'm going to show how to retrieve only the public ones, but obviously a similar approach can be used to get those with a stronger visibility. Now that I've clarified that, please check the following code sample. It uses another reflection method, called "getMethods()," which returns an array containing the public methods of the previous "User" class: // 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 all public methods of reflected class print_r($reflector->getMethods(ReflectionMethod::IS_PUBLIC)); // displays Array ( [0] => ReflectionMethod Object ( [name] => __construct [class] => User ) [1] => ReflectionMethod Object ( [name] => setId [class] => User ) [2] => ReflectionMethod Object ( [name] => getId [class] => User ) [3] => ReflectionMethod Object ( [name] => setFirstName [class] => User ) [4] => ReflectionMethod Object ( [name] => getFirstName [class] => User ) [5] => ReflectionMethod Object ( [name] => setLastName [class] => User ) [6] => ReflectionMethod Object ( [name] => getLastName [class] => User ) [7] => ReflectionMethod Object ( [name] => setEmail [class] => User ) [8] => ReflectionMethod Object ( [name] => getEmail [class] => User ) ) As you can see, the "IS_PUBLIC" constant of the ReflectionMethod class is inputted into "getMethods()" to get all of the public methods of "User." This outputs an array that naturally can be traversed by using a regular "foreach" construct. In a similar way, I could have passed to "getMethods()" the "IS_PROTECTED" and "IS_PRIVATE" constants to retrieve the protected and private methods respectively, but since this process is fairly straightforward, it will be left as homework for you. And with this last example, we've come to the end of this third part of the series. As usual, feel free to tweak all of the code samples included in this tutorial. Doing this will hopefully will arm yourself with a more solid background in using the PHP reflection API. Final thoughts That's all for now. In this third installment of the series you learned how to take advantage of the functionality provided by the PHP reflection API to retrieve useful information about the methods defined by a reflected class. In the examples shown, the power of reflection was used not only to get the names of those methods, but for determining their level of visibility. Of course, it's feasible to manipulate reflected methods in many more useful ways, but this will be left as a practical exercise for you. In the forthcoming part, I'm going to teach you how to use reflection to work with properties of a class, which will be a very juicy topic, trust me. Don't miss the upcoming article!
blog comments powered by Disqus |
|
|
|
|
|
|
|