| I. | Na początek | |
| II. | Instalacja i konfiguracja | |
| III. | Opis języka | |
| IV. | Bezpieczeństwo | |
| V. | Możliwości | |
| VI. | Opis funkcji | |
| VII. | Zend API | |
| VIII. | PHP API: Interfejs rozszerzeń | |
| X. | Dodatki | |
Copyright © 1997-2006 the PHP Documentation Group
PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.
The reflection API is an object-oriented extension to the Zend Engine, consisting of the following classes:
<?php |
Notatka: For details on these classes, have a look at the next chapters.
If we were to execute the code in the example below:
The ReflectionFunction class lets you reverse-engineer functions.
<?php |
Notatka: invokeArgs() was added in PHP 5.1.0.
To introspect a function, you will first have to create an instance of the ReflectionFunction class. You can then call any of the above methods on this instance.
Przykład 19-32. Using the ReflectionFunction class
|
Notatka: The method invoke() accepts a variable number of arguments which are passed to the function just as in call_user_func().
The ReflectionParameter class retrieves information about a function's or method's parameters.
<?php |
Notatka: getDefaultValue(), isDefaultValueAvailable(), isOptional() were added in PHP 5.1.0.
To introspect function parameters, you will first have to create an instance of the ReflectionFunction or ReflectionMethod classes and then use their getParameters() method to retrieve an array of parameters.
Przykład 19-33. Using the ReflectionParameter class
|
The ReflectionClass class lets you reverse-engineer classes.
<?php |
Notatka: hasConstant(), hasMethod(), hasProperty() were added in PHP 5.1.0.
To introspect a class, you will first have to create an instance of the ReflectionClass class. You can then call any of the above methods on this instance.
Przykład 19-34. Using the ReflectionClass class
|
Notatka: The method newInstance() accepts a variable number of arguments which are passed to the function just as in call_user_func().
Notatka: $class = new ReflectionClass('Foo'); $class->isInstance($arg) is equivalent to $arg instanceof Foo or is_a($arg, 'Foo').
The ReflectionMethod class lets you reverse-engineer class methods.
<?php |
To introspect a method, you will first have to create an instance of the ReflectionMethod class. You can then call any of the above methods on this instance.
Przykład 19-35. Using the ReflectionMethod class
|
Notatka: Trying to invoke private, protected or abstract methods will result in an exception being thrown from the invoke() method.
Notatka: For static methods as seen above, you should pass NULL as the first argument to invoke(). For non-static methods, pass an instance of the class.
The ReflectionProperty class lets you reverse-engineer class properties.
<?php |
To introspect a property, you will first have to create an instance of the ReflectionProperty class. You can then call any of the above methods on this instance.
Przykład 19-36. Using the ReflectionProperty class
|
Notatka: Trying to get or set private or protected class property's values will result in an exception being thrown.
The ReflectionExtension class lets you reverse-engineer extensions. You can retrieve all loaded extensions at runtime using the get_loaded_extensions().
<?php |
To introspect an extension, you will first have to create an instance of the ReflectionExtension class. You can then call any of the above methods on this instance.
Przykład 19-37. Using the ReflectionExtension class
|
In case you want to create specialized versions of the built-in classes (say, for creating colorized HTML when being exported, having easy-access member variables instead of methods or having utility methods), you may go ahead and extend them.
Przykład 19-38. Extending the built-in classes
|
Notatka: Caution: If you're overwriting the constructor, remember to call the parent's constructor _before_ any code you insert. Failing to do so will result in the following: Fatal error: Internal error: Failed to retrieve the reflection object
| Poprzedni | Spis treści | Następny |
| Comparing objects | Początek rozdziału | Type Hinting |