As with many other reflection processes shown in the other tutorials of this series, checking to see whether or not a given class is user-defined is only a matter of calling the appropriate reflective method. In this case, the method that will get this job done is called "isUserDefined()," and an example of its use is included below: // create instance of 'User' class $user = new User();
// create instance of Reflection class and pass in 'User' class as an argument
// check if the reflected class is a user-defined class if ($reflector->isUserDefined()) { echo 'Reflected class is a user-defined class'; // displays 'Reflected class is a user-defined class' } else { echo 'Reflected class is not a user-defined class'; } There you have it. Now, if you try the above script, it'll display a neat message on screen informing you that the "User" class is indeed a user-defined one. Definitely, grasping how this last method does its thing isn't brain surgery, but it should give you a more exact idea of the impressive capabilities offered by the PHP reflection API. And last but not least, if you're looking for a full reference for the API, I strongly recommend that you look up its official documentation, which is always the best source of information. Final thoughts It's hard to believe, but we've come to the end of this series. Hopefully, its tutorials have been pretty instructive. You learned how to work with some of the most important methods that come packaged with the PHP 5 reflection API, which can be much more helpful than it appears at first glance. While it's fair to say that all of the code samples shown in this series are only "tutorial examples," they should give you a clear idea of how to take advantage of the power of reflection when developing real-world applications. The moral of this story is simple: if you need to analyze in depth the internal structure of classes and interfaces, the reflection API is by far the best option to pick at the present time. See you in the next PHP tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|