cvs: phpdoc-ru /language/oop5 reflection.xml
| From: | Alexey Romanenko | Date: | Tue, 15 Apr 2008 20:56:54 +0000 |
| Subject: | cvs: phpdoc-ru /language/oop5 reflection.xml | ||
| Groups: | php.doc.ru | ||
| Request: | Send a blank email to doc-ru+get-924@lists.php.net to get a copy of this message | ||
santiago Tue Apr 15 20:56:54 2008 UTC
Added files:
/phpdoc-ru/language/oop5 reflection.xml
Log:
add new translation by Alexey Romanenko
http://cvs.php.net/viewvc.cgi/phpdoc-ru/language/oop5/reflection.xml?view=markup&rev=1.1 Index: phpdoc-ru/language/oop5/reflection.xml +++ phpdoc-ru/language/oop5/reflection.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Revision: 1.1 $ --> <sect1 xml:id="language.oop5.reflection" xmlns="http://docbook.org/ns/docbook"> <title>РеÑлекÑиÑ</title> <sect2 xml:id="language.oop5.reflection.toc"> <title>СодеÑжание</title> <simplelist> <member><link linkend="language.oop5.reflection.introduction">Ðведение</link></member> <member><link linkend="language.oop5.reflection.reflector">ÐнÑеÑÑÐµÐ¹Ñ Reflector</link></member> <member><link linkend="language.oop5.reflection.reflectionexception">ÐлаÑÑ ReflectionException</link></member> <member><link linkend="language.oop5.reflection.reflectionfunction">ÐлаÑÑ ReflectionFunction</link></member> <member><link linkend="language.oop5.reflection.reflectionparameter">ÐлаÑÑ ReflectionParameter</link></member> <member><link linkend="language.oop5.reflection.reflectionclass">ÐлаÑÑ ReflectionClass</link></member> <member><link linkend="language.oop5.reflection.reflectionobject">ÐлаÑÑ ReflectionObject</link></member> <member><link linkend="language.oop5.reflection.reflectionmethod">ÐлаÑÑ ReflectionMethod</link></member> <member><link linkend="language.oop5.reflection.reflectionproperty">ÐлаÑÑ ReflectionProperty</link></member> <member><link linkend="language.oop5.reflection.reflectionextension">ÐлаÑÑ ReflectionExtension</link></member> <member><link linkend="language.oop5.reflection.extending">РаÑÑиÑение клаÑÑов ÑеÑлекÑии</link></member> </simplelist> </sect2> <sect2 xml:id="language.oop5.reflection.introduction"> <title>Ðведение</title> <para> PHP 5 Ð¸Ð¼ÐµÐµÑ Ð¿Ð¾Ð»Ð½Ñй Ð½Ð°Ð±Ð¾Ñ API ÑеÑлекÑии, коÑоÑÑй пÑедоÑÑавлÑÐµÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ ÐºÐ»Ð°ÑÑÑ, инÑеÑÑейÑÑ, ÑÑнкÑии и меÑодÑ, в Ñом ÑиÑле и ÑаÑÑиÑениÑ. Рдополнение к ÑÑомÑ, API ÑеÑлекÑии пÑÐµÐ´Ð»Ð°Ð³Ð°ÐµÑ Ð¿ÑÑи воÑÑÑÐ°Ð½Ð¾Ð²Ð»ÐµÐ½Ð¸Ñ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑиев Ð´Ð»Ñ ÑÑнкÑий, клаÑÑов и меÑодов. </para> <para> API ÑеÑлекÑии - ÑÑо обÑекÑно-оÑиенÑиÑованное ÑаÑÑиÑение Ð´Ð»Ñ Zend Engine, ÑоÑÑоÑÑее из ÑледÑÑÑÐ¸Ñ ÐºÐ»Ð°ÑÑов: </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { } ?> ]]> </programlisting> </informalexample> <note> <simpara> Ðолее подÑобно ÑÑи клаÑÑÑ ÑаÑÑмаÑÑиваÑÑÑÑ Ð² ÑледÑÑÑÐ¸Ñ ÑаÑÑÑÑ . </simpara> </note> <para> ÐÑли Ð¼Ñ Ð²Ñполним код из пÑимеÑа ниже: <example> <title>ÐÑновное иÑполÑзование API ÑеÑлекÑии</title> <programlisting role='php'> <![CDATA[ <?php Reflection::export(new ReflectionClass('Exception')); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Class [ <internal> class Exception ] { - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [6] { Property [ <default> protected $message ] Property [ <default> private $string ] Property [ <default> protected $code ] Property [ <default> protected $file ] Property [ <default> protected $line ] Property [ <default> private $trace ] } - Methods [9] { Method [ <internal> final private method __clone ] { } Method [ <internal, ctor> public method __construct ] { - Parameters [2] { Parameter #0 [ <optional> $message ] Parameter #1 [ <optional> $code ] } } Method [ <internal> final public method getMessage ] { } Method [ <internal> final public method getCode ] { } Method [ <internal> final public method getFile ] { } Method [ <internal> final public method getLine ] { } Method [ <internal> final public method getTrace ] { } Method [ <internal> final public method getTraceAsString ] { } Method [ <internal> public method __toString ] { } } } ]]> </screen> </example> </para> </sect2> <sect2 xml:id="language.oop5.reflection.reflector"> <title><classname>Reflector</classname></title> <para> <classname>Reflector</classname> ÑÑо инÑеÑÑейÑ, коÑоÑÑй имплеменÑиÑÑÐµÑ Ð²Ñе ÑкÑпоÑÑиÑÑемÑе клаÑÑÑ Reflection. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php interface Reflector { public string __toString() public static string export() } ?> ]]> </programlisting> </informalexample> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionexception"> <title><classname>ReflectionException</classname></title> <para> <classname>ReflectionException</classname> ÑаÑÑиÑÑÐµÑ ÑÑандаÑÑнÑй клаÑÑ <link linkend="language.exceptions">Exception</link> и его вÑбÑаÑÑÐ²Ð°ÐµÑ Reflection API. ÐÐ¸ÐºÐ°ÐºÐ¸Ñ ÑпеÑиалÑнÑÑ Ð¼ÐµÑодов или ÑвойÑÑв здеÑÑ Ð½Ðµ пÑедÑÑавлено. </para> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionfunction"> <title><classname>ReflectionFunction</classname></title> <para> ÐлаÑÑ <classname>ReflectionFunction</classname> позволÑÐµÑ Ð²Ð°Ð¼ пеÑепÑоекÑиÑоваÑÑ ÑÑнкÑии. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector { final private __clone() public void __construct(string name) public string __toString() public static string export(string name, bool return) public string getName() public bool isInternal() public bool isDisabled() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public mixed invoke([mixed args [, ...]]) public mixed invokeArgs(array args) public bool returnsReference() public ReflectionParameter[] getParameters() public int getNumberOfParameters() public int getNumberOfRequiredParameters() } ?> ]]> </programlisting> </informalexample> <para> РодиÑелÑÑкий клаÑÑ <classname>ReflectionFunctionAbstract</classname> Ð¸Ð¼ÐµÐµÑ Ñакие же меÑодÑ, за иÑклÑÑением <function>invoke</function>, <function>invokeArgs</function>, <function>export</function> и <function>isDisabled</function>. </para> <note> <simpara> <function>getNumberOfParameters</function> и <function>getNumberOfRequiredParameters</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.0.3, а <function>invokeArgs</function> бÑл добавлен в PHP 5.1.0. </simpara> </note> <para> ÐнализиÑÑÑ ÑÑнкÑиÑ, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionFunction</classname>. ÐаÑем, в ÑÑом обÑекÑе Ð²Ñ ÑможеÑе вÑзÑваÑÑ Ð»Ñбой из меÑодов ÑÑого клаÑÑа, ÑпомÑнÑÑÑÑ Ð²ÑÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionFunction</classname></title> <programlisting role='php'> <![CDATA[ <?php /** * ÐÑоÑÑой ÑÑеÑÑик * * @return int */ function counter() { static $c = 0; return $c++; } // Создание ÑкземплÑÑа клаÑÑа ReflectionFunction $func = new ReflectionFunction('counter'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "===> ФÑнкÑÐ¸Ñ %s '%s'\n". " обÑÑвлена в %s\n". " ÑÑÑоки Ñ %d по %d\n", $func->isInternal() ? 'internal' : 'user-defined', $func->getName(), $func->getFileName(), $func->getStartLine(), $func->getEndline() ); // ÐеÑаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑÐ¸Ñ Ð´Ð¾ÐºÑменÑаÑии printf("---> ÐокÑменÑаÑиÑ:\n %s\n", var_export($func->getDocComment(), 1)); // ÐеÑаÑÑ ÑÑаÑиÑеÑкой пеÑеменной, еÑли она ÑÑÑеÑÑвÑÐµÑ if ($statics = $func->getStaticVariables()) { printf("---> СÑаÑиÑеÑÐºÐ°Ñ Ð¿ÐµÑеменнаÑ: %s\n", var_export($statics, 1)); } // ÐÑзов ÑÑнкÑии printf("---> РезÑлÑÑÐ°Ñ Ð²Ñзова в: "); var_dump($func->invoke()); // Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе пÑедпоÑеÑÑÑ Ð´Ð»Ñ Ð¸ÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ Ð¼ÐµÑод export() echo "\nReflectionFunction::export() ÑезÑлÑÑаÑ:\n"; echo ReflectionFunction::export('counter'); ?> ]]> </programlisting> </example> <note> <simpara> ÐеÑод <function>invoke</function> пÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð¿ÐµÑеменное ÑиÑло аÑгÑменÑов, коÑоÑÑе пеÑедаÑÑÑÑ Ð² ÑÑнкÑиÑ, Ñакже, как в <function>call_user_func</function>. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionparameter"> <title><classname>ReflectionParameter</classname></title> <para> ÐлаÑÑ <classname>ReflectionParameter</classname> возвÑаÑÐ°ÐµÑ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾ паÑамеÑÑÐ°Ñ ÑÑнкÑии или меÑода. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionParameter implements Reflector { final private __clone() public void __construct(string function, string parameter) public string __toString() public static string export(mixed function, mixed parameter, bool return) public string getName() public bool isPassedByReference() public ReflectionClass getDeclaringClass() public ReflectionClass getClass() public bool isArray() public bool allowsNull() public bool isPassedByReference() public bool isOptional() public bool isDefaultValueAvailable() public mixed getDefaultValue() } ?> ]]> </programlisting> </informalexample> <note> <para> <function>getDefaultValue</function>, <function>isDefaultValueAvailable</function> и <function>isOptional</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.0.3, а <function>isArray</function> бÑл добавлен в PHP 5.1.0. <function>getDeclaringFunction</function> и <function>getPosition</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.2.3. </para> </note> <para> ÐнализиÑÑÑ Ð¿Ð°ÑамеÑÑÑ ÑÑнкÑии, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionFunction</classname> или <classname>ReflectionMethod</classname> и заÑем иÑполÑзоваÑÑ Ð¸Ñ Ð¼ÐµÑод <function>getParameters</function> Ð´Ð»Ñ Ð¿Ð¾Ð»ÑÑÐµÐ½Ð¸Ñ Ð¼Ð°ÑÑива паÑамеÑÑов. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionParameter</classname></title> <programlisting role='php'> <![CDATA[ <?php function foo($a, $b, $c) { } function bar(Exception $a, &$b, $c) { } function baz(ReflectionFunction $a, $b = 1, $c = null) { } function abc() { } // Создание ÑкземплÑÑа клаÑÑа ReflectionFunction // Ñ Ð¿Ð°ÑамеÑÑом, заданнÑм из командной ÑÑÑоки. $reflect = new ReflectionFunction($argv[1]); echo $reflect; foreach ($reflect->getParameters() as $i => $param) { printf( "-- ÐаÑамеÑÑ #%d: %s {\n". " ÐлаÑÑ: %s\n". " ÐопÑÑкаÑÑ NULL: %s\n". " ÐеÑедан по ÑÑÑлке: %s\n". " ÐбÑзаÑелÑнÑй?: %s\n". "}\n", $i, $param->getName(), var_export($param->getClass(), 1), var_export($param->allowsNull(), 1), var_export($param->isPassedByReference(), 1), $param->isOptional() ? 'да' : 'неÑ' ); } ?> ]]> </programlisting> </example> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionclass"> <title><classname>ReflectionClass</classname></title> <para> ÐлаÑÑ <classname>ReflectionClass</classname> позволÑÐµÑ Ð²Ð°Ð¼ пеÑепÑоекÑиÑоваÑÑ ÐºÐ»Ð°ÑÑÑ Ð¸ инÑеÑÑейÑÑ. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionClass implements Reflector { final private __clone() public void __construct(string name) public string __toString() public static string export(mixed class, bool return) public string getName() public bool isInternal() public bool isUserDefined() public bool isInstantiable() public bool hasConstant(string name) public bool hasMethod(string name) public bool hasProperty(string name) public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public ReflectionMethod getConstructor() public ReflectionMethod getMethod(string name) public ReflectionMethod[] getMethods() public ReflectionProperty getProperty(string name) public ReflectionProperty[] getProperties() public array getConstants() public mixed getConstant(string name) public ReflectionClass[] getInterfaces() public bool isInterface() public bool isAbstract() public bool isFinal() public int getModifiers() public bool isInstance(stdclass object) public stdclass newInstance(mixed args) public stdclass newInstanceArgs(array args) public ReflectionClass getParentClass() public bool isSubclassOf(ReflectionClass class) public array getStaticProperties() public mixed getStaticPropertyValue(string name [, mixed default]) public void setStaticPropertyValue(string name, mixed value) public array getDefaultProperties() public bool isIterateable() public bool implementsInterface(string name) public ReflectionExtension getExtension() public string getExtensionName() } ?> ]]> </programlisting> </informalexample> <note> <simpara> <function>hasConstant</function>, <function>hasMethod</function>, <function>hasProperty</function>, <function>getStaticPropertyValue</function> и <function>setStaticPropertyValue</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.1.0, а <function>newInstanceArgs</function> бÑл добавлен в PHP 5.1.3. </simpara> </note> <para> ÐнализиÑÑÑ ÐºÐ»Ð°ÑÑ, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionClass</classname>. ÐаÑем, Ð²Ñ ÑможеÑе в ÑÑом обÑекÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionClass</classname></title> <programlisting role='php'> <![CDATA[ <?php interface Serializable { // ... } class Object { // ... } /** * ÐлаÑÑ ÑÑеÑÑик */ class Counter extends Object implements Serializable { const START = 0; private static $c = Counter::START; /** * ÐÑзваÑÑ ÑÑеÑÑик * * @access public * @return int */ public function count() { return self::$c++; } } // Создание ÑкземплÑÑа клаÑÑа ReflectionClass $class = new ReflectionClass('Counter'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "===> %s%s%s %s '%s' [extends %s]\n" . " обÑÑвлен в %s\n" . " ÑÑÑоки Ñ %d по %d\n" . " Ð¸Ð¼ÐµÐµÑ Ð¼Ð¾Ð´Ð¸ÑикаÑоÑÑ %d [%s]\n", $class->isInternal() ? 'internal' : 'user-defined', $class->isAbstract() ? ' abstract' : '', $class->isFinal() ? ' final' : '', $class->isInterface() ? 'interface' : 'class', $class->getName(), var_export($class->getParentClass(), 1), $class->getFileName(), $class->getStartLine(), $class->getEndline(), $class->getModifiers(), implode(' ', Reflection::getModifierNames($class->getModifiers())) ); // ÐеÑаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑÐ¸Ñ Ð¸Ð· докÑменÑаÑии printf("---> ÐокÑменÑаÑиÑ:\n %s\n", var_export($class->getDocComment(), 1)); // ÐеÑаÑÑ ÑÐµÑ Ð¸Ð½ÑеÑÑейÑов, коÑоÑÑе имплеменÑиÑÑÑÑ ÑÑÐ¾Ñ ÐºÐ»Ð°ÑÑ printf("---> ÐмплеменÑаÑиÑ:\n %s\n", var_export($class->getInterfaces(), 1)); // ÐеÑаÑÑ ÐºÐ¾Ð½ÑÑÐ°Ð½Ñ ÐºÐ»Ð°ÑÑа printf("---> ÐонÑÑанÑÑ: %s\n", var_export($class->getConstants(), 1)); // ÐеÑаÑÑ ÑвойÑÑв клаÑÑа printf("---> СвойÑÑва: %s\n", var_export($class->getProperties(), 1)); // ÐеÑаÑÑ Ð¼ÐµÑодов клаÑÑа printf("---> ÐеÑодÑ: %s\n", var_export($class->getMethods(), 1)); // ÐÑли еÑÑÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа, Ñо Ñоздаем его if ($class->isInstantiable()) { $counter = $class->newInstance(); echo '---> $counter инÑÑанÑиÑован? '; echo $class->isInstance($counter) ? 'да' : 'неÑ'; echo "\n---> new Object() инÑÑанÑиÑован? "; echo $class->isInstance(new Object()) ? 'да' : 'неÑ'; } ?> ]]> </programlisting> </example> <note> <simpara> ÐеÑод <function>newInstance</function> пÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð¿ÐµÑеменное ÑиÑло аÑгÑменÑов, коÑоÑÑе пеÑедаÑÑÑÑ Ð² ÑÑнкÑиÑ, Ñакже, как в <function>call_user_func</function>. </simpara> </note> <note> <simpara> <literal>$class = new ReflectionClass('Foo'); $class->isInstance($arg)</literal> ÑквиваленÑно <literal>$arg instanceof Foo</literal> или <literal>is_a($arg, 'Foo')</literal>. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionobject"> <title><classname>ReflectionObject</classname></title> <para> ÐлаÑÑ <classname>ReflectionObject</classname> позволÑÐµÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ Ð¾Ð±ÑекÑÑ. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionObject extends ReflectionClass { final private __clone() public void __construct(mixed object) public string __toString() public static string export(mixed object, bool return) } ?> ]]> </programlisting> </informalexample> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionmethod"> <title><classname>ReflectionMethod</classname></title> <para> ÐлаÑÑ <classname>ReflectionMethod</classname> позволÑÐµÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ Ð¼ÐµÑодÑ. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { public void __construct(mixed class, string name) public string __toString() public static string export(mixed class, string name, bool return) public mixed invoke(stdclass object [, mixed args [, ...]]) public mixed invokeArgs(stdclass object, array args) public bool isFinal() public bool isAbstract() public bool isPublic() public bool isPrivate() public bool isProtected() public bool isStatic() public bool isConstructor() public bool isDestructor() public int getModifiers() public ReflectionClass getDeclaringClass() // УнаÑледовано из ReflectionFunctionAbstract final private __clone() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public bool returnsReference() public ReflectionParameter[] getParameters() public int getNumberOfParameters() public int getNumberOfRequiredParameters() } ?> ]]> </programlisting> </informalexample> <para> ÐнализиÑÑÑ Ð¼ÐµÑод, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionMethod</classname>. ÐаÑем, Ð²Ñ ÑможеÑе в ÑÑом обÑекÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionMethod</classname></title> <programlisting role='php'> <![CDATA[ <?php class Counter { private static $c = 0; /** * УвелиÑение ÑÑеÑÑика * * @final * @static * @access public * @return int */ final public static function increment() { return ++self::$c; } } // Создание ÑкземплÑÑа клаÑÑа ReflectionMethod $method = new ReflectionMethod('Counter', 'increment'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "===> %s%s%s%s%s%s%s меÑод '%s' (which is %s)\n" . " обÑÑвлен в %s\n" . " ÑÑÑоки Ñ %d по %d\n" . " Ð¸Ð¼ÐµÐµÑ Ð¼Ð¾Ð´Ð¸ÑикаÑоÑÑ %d[%s]\n", $method->isInternal() ? 'internal' : 'user-defined', $method->isAbstract() ? ' abstract' : '', $method->isFinal() ? ' final' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' static' : '', $method->getName(), $method->isConstructor() ? 'the constructor' : 'a regular method', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers())) ); // ÐеÑаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑÐ¸Ñ Ð¸Ð· докÑменÑаÑии printf("---> ÐокÑменÑаÑиÑ:\n %s\n", var_export($method->getDocComment(), 1)); // ÐеÑаÑÑ ÑÑаÑиÑеÑкой пеÑеменной, еÑли она ÑÑÑеÑÑвÑÐµÑ if ($statics= $method->getStaticVariables()) { printf("---> СÑаÑиÑеÑÐºÐ°Ñ Ð¿ÐµÑеменнаÑ: %s\n", var_export($statics, 1)); } // ÐÑзов меÑода printf("---> РезÑлÑÑÐ°Ñ Ð²Ñзова в: "); var_dump($method->invoke(NULL)); ?> ]]> </programlisting> </example> <note> <simpara> ÐопÑÑка вÑзова закÑÑÑого, заÑиÑенного или абÑÑÑакÑного меÑода пÑÐ¸Ð²ÐµÐ´ÐµÑ Ðº вÑбÑоÑÑ Ð¸ÑклÑÑÐµÐ½Ð¸Ñ Ð¸Ð· меÑода <function>invoke</function>. </simpara> </note> <note> <simpara> ÐÐ»Ñ ÑÑаÑиÑеÑÐºÐ¸Ñ Ð¼ÐµÑодов, опиÑаннÑÑ Ð²ÑÑе, Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ Ð¿ÐµÑедаÑÑ NULL в каÑеÑÑве пеÑвого аÑгÑменÑа в <function>invoke</function>. ÐÐ»Ñ Ð½ÐµÑÑаÑиÑеÑÐºÐ¸Ñ Ð¼ÐµÑодов нÑжно пеÑедаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionproperty"> <title><classname>ReflectionProperty</classname></title> <para> ÐлаÑÑ <classname>ReflectionProperty</classname> позволÑÐµÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ ÑвойÑÑва клаÑÑа. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionProperty implements Reflector { final private __clone() public void __construct(mixed class, string name) public string __toString() public static string export(mixed class, string name, bool return) public string getName() public bool isPublic() public bool isPrivate() public bool isProtected() public bool isStatic() public bool isDefault() public int getModifiers() public mixed getValue(stdclass object) public void setValue(stdclass object, mixed value) public ReflectionClass getDeclaringClass() public string getDocComment() } ?> ]]> </programlisting> </informalexample> <note> <simpara> <function>getDocComment</function> бÑл добавлен в PHP 5.1.0. </simpara> </note> <para> ÐнализиÑÑÑ ÑвойÑÑво, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionProperty</classname>. ÐаÑем, Ð²Ñ ÑможеÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе, в ÑÑом обÑекÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionProperty</classname></title> <programlisting role='php'> <![CDATA[ <?php class String { public $length = 5; } // Создание ÑкземплÑÑа клаÑÑа ReflectionProperty $prop = new ReflectionProperty('String', 'length'); // Print out basic information printf( "===> %s%s%s%s ÑвойÑÑво '%s' (коÑоÑое бÑло %s)\n" . " Ð¸Ð¼ÐµÐµÑ Ð¼Ð¾Ð´Ð¸ÑикаÑоÑÑ %s\n", $prop->isPublic() ? ' public' : '', $prop->isPrivate() ? ' private' : '', $prop->isProtected() ? ' protected' : '', $prop->isStatic() ? ' static' : '', $prop->getName(), $prop->isDefault() ? 'обÑÑвлено во вÑÐµÐ¼Ñ ÐºÐ¾Ð¼Ð¿Ð¸Ð»ÑÑии' : 'Ñоздано во вÑÐµÐ¼Ñ Ð²ÑполнениÑ', var_export(Reflection::getModifierNames($prop->getModifiers()), 1) ); // Создание ÑкземплÑÑа String $obj= new String(); // ÐолÑÑение ÑекÑÑего знаÑÐµÐ½Ð¸Ñ printf("---> ÐнаÑение: "); var_dump($prop->getValue($obj)); // Ðзменение знаÑÐµÐ½Ð¸Ñ $prop->setValue($obj, 10); printf("---> УÑÑановка знаÑÐµÐ½Ð¸Ñ 10, новое знаÑение Ñавно: "); var_dump($prop->getValue($obj)); // Ðамп обÑекÑа var_dump($obj); ?> ]]> </programlisting> </example> <note> <simpara> ÐопÑÑка вÑзова закÑÑÑого или заÑиÑенного ÑвойÑÑва клаÑÑа пÑÐ¸Ð²ÐµÐ´ÐµÑ Ðº вÑбÑоÑÑ Ð¸ÑклÑÑениÑ. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionextension"> <title><classname>ReflectionExtension</classname></title> <para> ÐлаÑÑ <classname>ReflectionExtension</classname> позволÑÐµÑ Ð²Ð°Ð¼ пеÑепÑоекÑиÑоваÑÑ ÑаÑÑиÑениÑ. ÐÑ Ð¼Ð¾Ð¶ÐµÑе воÑÑÑановиÑÑ Ð²Ñе загÑÑженнÑе ÑаÑÑиÑÐµÐ½Ð¸Ñ Ð²Ð¾ вÑÐµÐ¼Ñ Ð²ÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ Ð¸ÑполÑзÑÑ <function>get_loaded_extensions</function>. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionExtension implements Reflector { final private __clone() public void __construct(string name) public string __toString() public static string export(string name, bool return) public string getName() public string getVersion() public ReflectionFunction[] getFunctions() public array getConstants() public array getINIEntries() public ReflectionClass[] getClasses() public array getClassNames() public string info() } ?> ]]> </programlisting> </informalexample> <para> ÐнализиÑÑÑ ÑаÑÑиÑение, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionExtension</classname>. ÐаÑем, Ð²Ñ ÑможеÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе, в ÑÑом обÑекÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionExtension</classname></title> <programlisting role='php'> <![CDATA[ <?php // Создание ÑкземплÑÑа клаÑÑа ReflectionProperty $ext = new ReflectionExtension('standard'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "ÐÐ¼Ñ : %s\n" . "ÐеÑÑÐ¸Ñ : %s\n" . "ФÑнкÑии : [%d] %s\n" . "ÐонÑÑанÑÑ : [%d] %s\n" . "ÐиÑекÑÐ¸Ð²Ñ INI : [%d] %s\n" . "ÐлаÑÑÑ : [%d] %s\n", $ext->getName(), $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', sizeof($ext->getFunctions()), var_export($ext->getFunctions(), 1), sizeof($ext->getConstants()), var_export($ext->getConstants(), 1), sizeof($ext->getINIEntries()), var_export($ext->getINIEntries(), 1), sizeof($ext->getClassNames()), var_export($ext->getClassNames(), 1) ); ?> ]]> </programlisting> </example> </sect2> <sect2 xml:id="language.oop5.reflection.extending"> <title>РаÑÑиÑение клаÑÑов ÑеÑлекÑии</title> <para> Ð ÑлÑÑае, когда Ð²Ñ Ð·Ð°Ñ Ð¾ÑиÑе ÑоздаÑÑ ÑпеÑиализиÑованнÑе веÑÑии вÑÑÑоеннÑÑ ÐºÐ»Ð°ÑÑов (напÑимеÑ, Ð´Ð»Ñ ÑÐ¾Ð·Ð´Ð°Ð½Ð¸Ñ Ð¿Ð¾Ð´ÑвеÑки HTML пÑи ÑкÑпоÑÑе Ñдобно имеÑÑ Ð»ÐµÐ³ÐºÐ¾Ð´Ð¾ÑÑÑпнÑе пеÑеменнÑе взамен меÑодов, либо ÑÑилиÑÑ Ð¼ÐµÑодов), Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе пойÑи далÑÑе и ÑаÑÑиÑиÑÑ Ð¸Ñ . </para> <example> <title>РаÑÑиÑение вÑÑÑоеннÑÑ ÐºÐ»Ð°ÑÑов</title> <programlisting role='php'> <![CDATA[ <?php /** * Свой клаÑÑ Reflection_Method */ class My_Reflection_Method extends ReflectionMethod { public $visibility = array(); public function __construct($o, $m) { parent::__construct($o, $m); $this->visibility = Reflection::getModifierNames($this->getModifiers()); } } /** * Ðемо-клаÑÑ #1 * */ class T { protected function x() {} } /** * Ðемо-клаÑÑ #2 * */ class U extends T { function x() {} } // ÐÑвод инÑоÑмаÑии var_dump(new My_Reflection_Method('U', 'x')); ?> ]]> </programlisting> </example> <note> <simpara> Ðнимание! ÐÑли Ð²Ñ Ð¿ÐµÑепиÑÑваеÑе конÑÑÑÑкÑоÑ, Ñо не забÑдÑÑе вÑзÑваÑÑ ÐºÐ¾Ð½ÑÑÑÑкÑÐ¾Ñ ÑодиÑÐµÐ»Ñ _пеÑед_ лÑбÑм кодом, коÑоÑÑй Ð²Ñ Ð·Ð°Ñем вÑÑавиÑе. Ð ÑлÑÑае невÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ ÑÑого ÑÑÐ»Ð¾Ð²Ð¸Ñ ÑезÑлÑÑÐ°Ñ Ð±ÑÐ´ÐµÑ ÑледÑÑÑим: <literal> Fatal error: Internal error: Failed to retrieve the reflection object </literal> </simpara> </note> </sect2> </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
http://cvs.php.net/viewvc.cgi/phpdoc-ru/language/oop5/reflection.xml?view=markup&rev=1.1 Index: phpdoc-ru/language/oop5/reflection.xml +++ phpdoc-ru/language/oop5/reflection.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Revision: 1.1 $ --> <sect1 xml:id="language.oop5.reflection" xmlns="http://docbook.org/ns/docbook"> <title>РеÑлекÑиÑ</title> <sect2 xml:id="language.oop5.reflection.toc"> <title>СодеÑжание</title> <simplelist> <member><link linkend="language.oop5.reflection.introduction">Ðведение</link></member> <member><link linkend="language.oop5.reflection.reflector">ÐнÑеÑÑÐµÐ¹Ñ Reflector</link></member> <member><link linkend="language.oop5.reflection.reflectionexception">ÐлаÑÑ ReflectionException</link></member> <member><link linkend="language.oop5.reflection.reflectionfunction">ÐлаÑÑ ReflectionFunction</link></member> <member><link linkend="language.oop5.reflection.reflectionparameter">ÐлаÑÑ ReflectionParameter</link></member> <member><link linkend="language.oop5.reflection.reflectionclass">ÐлаÑÑ ReflectionClass</link></member> <member><link linkend="language.oop5.reflection.reflectionobject">ÐлаÑÑ ReflectionObject</link></member> <member><link linkend="language.oop5.reflection.reflectionmethod">ÐлаÑÑ ReflectionMethod</link></member> <member><link linkend="language.oop5.reflection.reflectionproperty">ÐлаÑÑ ReflectionProperty</link></member> <member><link linkend="language.oop5.reflection.reflectionextension">ÐлаÑÑ ReflectionExtension</link></member> <member><link linkend="language.oop5.reflection.extending">РаÑÑиÑение клаÑÑов ÑеÑлекÑии</link></member> </simplelist> </sect2> <sect2 xml:id="language.oop5.reflection.introduction"> <title>Ðведение</title> <para> PHP 5 Ð¸Ð¼ÐµÐµÑ Ð¿Ð¾Ð»Ð½Ñй Ð½Ð°Ð±Ð¾Ñ API ÑеÑлекÑии, коÑоÑÑй пÑедоÑÑавлÑÐµÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ ÐºÐ»Ð°ÑÑÑ, инÑеÑÑейÑÑ, ÑÑнкÑии и меÑодÑ, в Ñом ÑиÑле и ÑаÑÑиÑениÑ. Рдополнение к ÑÑомÑ, API ÑеÑлекÑии пÑÐµÐ´Ð»Ð°Ð³Ð°ÐµÑ Ð¿ÑÑи воÑÑÑÐ°Ð½Ð¾Ð²Ð»ÐµÐ½Ð¸Ñ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑиев Ð´Ð»Ñ ÑÑнкÑий, клаÑÑов и меÑодов. </para> <para> API ÑеÑлекÑии - ÑÑо обÑекÑно-оÑиенÑиÑованное ÑаÑÑиÑение Ð´Ð»Ñ Zend Engine, ÑоÑÑоÑÑее из ÑледÑÑÑÐ¸Ñ ÐºÐ»Ð°ÑÑов: </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { } ?> ]]> </programlisting> </informalexample> <note> <simpara> Ðолее подÑобно ÑÑи клаÑÑÑ ÑаÑÑмаÑÑиваÑÑÑÑ Ð² ÑледÑÑÑÐ¸Ñ ÑаÑÑÑÑ . </simpara> </note> <para> ÐÑли Ð¼Ñ Ð²Ñполним код из пÑимеÑа ниже: <example> <title>ÐÑновное иÑполÑзование API ÑеÑлекÑии</title> <programlisting role='php'> <![CDATA[ <?php Reflection::export(new ReflectionClass('Exception')); ?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ Class [ <internal> class Exception ] { - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [6] { Property [ <default> protected $message ] Property [ <default> private $string ] Property [ <default> protected $code ] Property [ <default> protected $file ] Property [ <default> protected $line ] Property [ <default> private $trace ] } - Methods [9] { Method [ <internal> final private method __clone ] { } Method [ <internal, ctor> public method __construct ] { - Parameters [2] { Parameter #0 [ <optional> $message ] Parameter #1 [ <optional> $code ] } } Method [ <internal> final public method getMessage ] { } Method [ <internal> final public method getCode ] { } Method [ <internal> final public method getFile ] { } Method [ <internal> final public method getLine ] { } Method [ <internal> final public method getTrace ] { } Method [ <internal> final public method getTraceAsString ] { } Method [ <internal> public method __toString ] { } } } ]]> </screen> </example> </para> </sect2> <sect2 xml:id="language.oop5.reflection.reflector"> <title><classname>Reflector</classname></title> <para> <classname>Reflector</classname> ÑÑо инÑеÑÑейÑ, коÑоÑÑй имплеменÑиÑÑÐµÑ Ð²Ñе ÑкÑпоÑÑиÑÑемÑе клаÑÑÑ Reflection. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php interface Reflector { public string __toString() public static string export() } ?> ]]> </programlisting> </informalexample> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionexception"> <title><classname>ReflectionException</classname></title> <para> <classname>ReflectionException</classname> ÑаÑÑиÑÑÐµÑ ÑÑандаÑÑнÑй клаÑÑ <link linkend="language.exceptions">Exception</link> и его вÑбÑаÑÑÐ²Ð°ÐµÑ Reflection API. ÐÐ¸ÐºÐ°ÐºÐ¸Ñ ÑпеÑиалÑнÑÑ Ð¼ÐµÑодов или ÑвойÑÑв здеÑÑ Ð½Ðµ пÑедÑÑавлено. </para> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionfunction"> <title><classname>ReflectionFunction</classname></title> <para> ÐлаÑÑ <classname>ReflectionFunction</classname> позволÑÐµÑ Ð²Ð°Ð¼ пеÑепÑоекÑиÑоваÑÑ ÑÑнкÑии. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector { final private __clone() public void __construct(string name) public string __toString() public static string export(string name, bool return) public string getName() public bool isInternal() public bool isDisabled() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public mixed invoke([mixed args [, ...]]) public mixed invokeArgs(array args) public bool returnsReference() public ReflectionParameter[] getParameters() public int getNumberOfParameters() public int getNumberOfRequiredParameters() } ?> ]]> </programlisting> </informalexample> <para> РодиÑелÑÑкий клаÑÑ <classname>ReflectionFunctionAbstract</classname> Ð¸Ð¼ÐµÐµÑ Ñакие же меÑодÑ, за иÑклÑÑением <function>invoke</function>, <function>invokeArgs</function>, <function>export</function> и <function>isDisabled</function>. </para> <note> <simpara> <function>getNumberOfParameters</function> и <function>getNumberOfRequiredParameters</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.0.3, а <function>invokeArgs</function> бÑл добавлен в PHP 5.1.0. </simpara> </note> <para> ÐнализиÑÑÑ ÑÑнкÑиÑ, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionFunction</classname>. ÐаÑем, в ÑÑом обÑекÑе Ð²Ñ ÑможеÑе вÑзÑваÑÑ Ð»Ñбой из меÑодов ÑÑого клаÑÑа, ÑпомÑнÑÑÑÑ Ð²ÑÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionFunction</classname></title> <programlisting role='php'> <![CDATA[ <?php /** * ÐÑоÑÑой ÑÑеÑÑик * * @return int */ function counter() { static $c = 0; return $c++; } // Создание ÑкземплÑÑа клаÑÑа ReflectionFunction $func = new ReflectionFunction('counter'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "===> ФÑнкÑÐ¸Ñ %s '%s'\n". " обÑÑвлена в %s\n". " ÑÑÑоки Ñ %d по %d\n", $func->isInternal() ? 'internal' : 'user-defined', $func->getName(), $func->getFileName(), $func->getStartLine(), $func->getEndline() ); // ÐеÑаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑÐ¸Ñ Ð´Ð¾ÐºÑменÑаÑии printf("---> ÐокÑменÑаÑиÑ:\n %s\n", var_export($func->getDocComment(), 1)); // ÐеÑаÑÑ ÑÑаÑиÑеÑкой пеÑеменной, еÑли она ÑÑÑеÑÑвÑÐµÑ if ($statics = $func->getStaticVariables()) { printf("---> СÑаÑиÑеÑÐºÐ°Ñ Ð¿ÐµÑеменнаÑ: %s\n", var_export($statics, 1)); } // ÐÑзов ÑÑнкÑии printf("---> РезÑлÑÑÐ°Ñ Ð²Ñзова в: "); var_dump($func->invoke()); // Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе пÑедпоÑеÑÑÑ Ð´Ð»Ñ Ð¸ÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ Ð¼ÐµÑод export() echo "\nReflectionFunction::export() ÑезÑлÑÑаÑ:\n"; echo ReflectionFunction::export('counter'); ?> ]]> </programlisting> </example> <note> <simpara> ÐеÑод <function>invoke</function> пÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð¿ÐµÑеменное ÑиÑло аÑгÑменÑов, коÑоÑÑе пеÑедаÑÑÑÑ Ð² ÑÑнкÑиÑ, Ñакже, как в <function>call_user_func</function>. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionparameter"> <title><classname>ReflectionParameter</classname></title> <para> ÐлаÑÑ <classname>ReflectionParameter</classname> возвÑаÑÐ°ÐµÑ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾ паÑамеÑÑÐ°Ñ ÑÑнкÑии или меÑода. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionParameter implements Reflector { final private __clone() public void __construct(string function, string parameter) public string __toString() public static string export(mixed function, mixed parameter, bool return) public string getName() public bool isPassedByReference() public ReflectionClass getDeclaringClass() public ReflectionClass getClass() public bool isArray() public bool allowsNull() public bool isPassedByReference() public bool isOptional() public bool isDefaultValueAvailable() public mixed getDefaultValue() } ?> ]]> </programlisting> </informalexample> <note> <para> <function>getDefaultValue</function>, <function>isDefaultValueAvailable</function> и <function>isOptional</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.0.3, а <function>isArray</function> бÑл добавлен в PHP 5.1.0. <function>getDeclaringFunction</function> и <function>getPosition</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.2.3. </para> </note> <para> ÐнализиÑÑÑ Ð¿Ð°ÑамеÑÑÑ ÑÑнкÑии, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionFunction</classname> или <classname>ReflectionMethod</classname> и заÑем иÑполÑзоваÑÑ Ð¸Ñ Ð¼ÐµÑод <function>getParameters</function> Ð´Ð»Ñ Ð¿Ð¾Ð»ÑÑÐµÐ½Ð¸Ñ Ð¼Ð°ÑÑива паÑамеÑÑов. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionParameter</classname></title> <programlisting role='php'> <![CDATA[ <?php function foo($a, $b, $c) { } function bar(Exception $a, &$b, $c) { } function baz(ReflectionFunction $a, $b = 1, $c = null) { } function abc() { } // Создание ÑкземплÑÑа клаÑÑа ReflectionFunction // Ñ Ð¿Ð°ÑамеÑÑом, заданнÑм из командной ÑÑÑоки. $reflect = new ReflectionFunction($argv[1]); echo $reflect; foreach ($reflect->getParameters() as $i => $param) { printf( "-- ÐаÑамеÑÑ #%d: %s {\n". " ÐлаÑÑ: %s\n". " ÐопÑÑкаÑÑ NULL: %s\n". " ÐеÑедан по ÑÑÑлке: %s\n". " ÐбÑзаÑелÑнÑй?: %s\n". "}\n", $i, $param->getName(), var_export($param->getClass(), 1), var_export($param->allowsNull(), 1), var_export($param->isPassedByReference(), 1), $param->isOptional() ? 'да' : 'неÑ' ); } ?> ]]> </programlisting> </example> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionclass"> <title><classname>ReflectionClass</classname></title> <para> ÐлаÑÑ <classname>ReflectionClass</classname> позволÑÐµÑ Ð²Ð°Ð¼ пеÑепÑоекÑиÑоваÑÑ ÐºÐ»Ð°ÑÑÑ Ð¸ инÑеÑÑейÑÑ. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionClass implements Reflector { final private __clone() public void __construct(string name) public string __toString() public static string export(mixed class, bool return) public string getName() public bool isInternal() public bool isUserDefined() public bool isInstantiable() public bool hasConstant(string name) public bool hasMethod(string name) public bool hasProperty(string name) public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public ReflectionMethod getConstructor() public ReflectionMethod getMethod(string name) public ReflectionMethod[] getMethods() public ReflectionProperty getProperty(string name) public ReflectionProperty[] getProperties() public array getConstants() public mixed getConstant(string name) public ReflectionClass[] getInterfaces() public bool isInterface() public bool isAbstract() public bool isFinal() public int getModifiers() public bool isInstance(stdclass object) public stdclass newInstance(mixed args) public stdclass newInstanceArgs(array args) public ReflectionClass getParentClass() public bool isSubclassOf(ReflectionClass class) public array getStaticProperties() public mixed getStaticPropertyValue(string name [, mixed default]) public void setStaticPropertyValue(string name, mixed value) public array getDefaultProperties() public bool isIterateable() public bool implementsInterface(string name) public ReflectionExtension getExtension() public string getExtensionName() } ?> ]]> </programlisting> </informalexample> <note> <simpara> <function>hasConstant</function>, <function>hasMethod</function>, <function>hasProperty</function>, <function>getStaticPropertyValue</function> и <function>setStaticPropertyValue</function> бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ Ð² PHP 5.1.0, а <function>newInstanceArgs</function> бÑл добавлен в PHP 5.1.3. </simpara> </note> <para> ÐнализиÑÑÑ ÐºÐ»Ð°ÑÑ, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionClass</classname>. ÐаÑем, Ð²Ñ ÑможеÑе в ÑÑом обÑекÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionClass</classname></title> <programlisting role='php'> <![CDATA[ <?php interface Serializable { // ... } class Object { // ... } /** * ÐлаÑÑ ÑÑеÑÑик */ class Counter extends Object implements Serializable { const START = 0; private static $c = Counter::START; /** * ÐÑзваÑÑ ÑÑеÑÑик * * @access public * @return int */ public function count() { return self::$c++; } } // Создание ÑкземплÑÑа клаÑÑа ReflectionClass $class = new ReflectionClass('Counter'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "===> %s%s%s %s '%s' [extends %s]\n" . " обÑÑвлен в %s\n" . " ÑÑÑоки Ñ %d по %d\n" . " Ð¸Ð¼ÐµÐµÑ Ð¼Ð¾Ð´Ð¸ÑикаÑоÑÑ %d [%s]\n", $class->isInternal() ? 'internal' : 'user-defined', $class->isAbstract() ? ' abstract' : '', $class->isFinal() ? ' final' : '', $class->isInterface() ? 'interface' : 'class', $class->getName(), var_export($class->getParentClass(), 1), $class->getFileName(), $class->getStartLine(), $class->getEndline(), $class->getModifiers(), implode(' ', Reflection::getModifierNames($class->getModifiers())) ); // ÐеÑаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑÐ¸Ñ Ð¸Ð· докÑменÑаÑии printf("---> ÐокÑменÑаÑиÑ:\n %s\n", var_export($class->getDocComment(), 1)); // ÐеÑаÑÑ ÑÐµÑ Ð¸Ð½ÑеÑÑейÑов, коÑоÑÑе имплеменÑиÑÑÑÑ ÑÑÐ¾Ñ ÐºÐ»Ð°ÑÑ printf("---> ÐмплеменÑаÑиÑ:\n %s\n", var_export($class->getInterfaces(), 1)); // ÐеÑаÑÑ ÐºÐ¾Ð½ÑÑÐ°Ð½Ñ ÐºÐ»Ð°ÑÑа printf("---> ÐонÑÑанÑÑ: %s\n", var_export($class->getConstants(), 1)); // ÐеÑаÑÑ ÑвойÑÑв клаÑÑа printf("---> СвойÑÑва: %s\n", var_export($class->getProperties(), 1)); // ÐеÑаÑÑ Ð¼ÐµÑодов клаÑÑа printf("---> ÐеÑодÑ: %s\n", var_export($class->getMethods(), 1)); // ÐÑли еÑÑÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа, Ñо Ñоздаем его if ($class->isInstantiable()) { $counter = $class->newInstance(); echo '---> $counter инÑÑанÑиÑован? '; echo $class->isInstance($counter) ? 'да' : 'неÑ'; echo "\n---> new Object() инÑÑанÑиÑован? "; echo $class->isInstance(new Object()) ? 'да' : 'неÑ'; } ?> ]]> </programlisting> </example> <note> <simpara> ÐеÑод <function>newInstance</function> пÑÐ¸Ð½Ð¸Ð¼Ð°ÐµÑ Ð¿ÐµÑеменное ÑиÑло аÑгÑменÑов, коÑоÑÑе пеÑедаÑÑÑÑ Ð² ÑÑнкÑиÑ, Ñакже, как в <function>call_user_func</function>. </simpara> </note> <note> <simpara> <literal>$class = new ReflectionClass('Foo'); $class->isInstance($arg)</literal> ÑквиваленÑно <literal>$arg instanceof Foo</literal> или <literal>is_a($arg, 'Foo')</literal>. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionobject"> <title><classname>ReflectionObject</classname></title> <para> ÐлаÑÑ <classname>ReflectionObject</classname> позволÑÐµÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ Ð¾Ð±ÑекÑÑ. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionObject extends ReflectionClass { final private __clone() public void __construct(mixed object) public string __toString() public static string export(mixed object, bool return) } ?> ]]> </programlisting> </informalexample> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionmethod"> <title><classname>ReflectionMethod</classname></title> <para> ÐлаÑÑ <classname>ReflectionMethod</classname> позволÑÐµÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ Ð¼ÐµÑодÑ. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { public void __construct(mixed class, string name) public string __toString() public static string export(mixed class, string name, bool return) public mixed invoke(stdclass object [, mixed args [, ...]]) public mixed invokeArgs(stdclass object, array args) public bool isFinal() public bool isAbstract() public bool isPublic() public bool isPrivate() public bool isProtected() public bool isStatic() public bool isConstructor() public bool isDestructor() public int getModifiers() public ReflectionClass getDeclaringClass() // УнаÑледовано из ReflectionFunctionAbstract final private __clone() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public bool returnsReference() public ReflectionParameter[] getParameters() public int getNumberOfParameters() public int getNumberOfRequiredParameters() } ?> ]]> </programlisting> </informalexample> <para> ÐнализиÑÑÑ Ð¼ÐµÑод, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionMethod</classname>. ÐаÑем, Ð²Ñ ÑможеÑе в ÑÑом обÑекÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionMethod</classname></title> <programlisting role='php'> <![CDATA[ <?php class Counter { private static $c = 0; /** * УвелиÑение ÑÑеÑÑика * * @final * @static * @access public * @return int */ final public static function increment() { return ++self::$c; } } // Создание ÑкземплÑÑа клаÑÑа ReflectionMethod $method = new ReflectionMethod('Counter', 'increment'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "===> %s%s%s%s%s%s%s меÑод '%s' (which is %s)\n" . " обÑÑвлен в %s\n" . " ÑÑÑоки Ñ %d по %d\n" . " Ð¸Ð¼ÐµÐµÑ Ð¼Ð¾Ð´Ð¸ÑикаÑоÑÑ %d[%s]\n", $method->isInternal() ? 'internal' : 'user-defined', $method->isAbstract() ? ' abstract' : '', $method->isFinal() ? ' final' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' static' : '', $method->getName(), $method->isConstructor() ? 'the constructor' : 'a regular method', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers())) ); // ÐеÑаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑÐ¸Ñ Ð¸Ð· докÑменÑаÑии printf("---> ÐокÑменÑаÑиÑ:\n %s\n", var_export($method->getDocComment(), 1)); // ÐеÑаÑÑ ÑÑаÑиÑеÑкой пеÑеменной, еÑли она ÑÑÑеÑÑвÑÐµÑ if ($statics= $method->getStaticVariables()) { printf("---> СÑаÑиÑеÑÐºÐ°Ñ Ð¿ÐµÑеменнаÑ: %s\n", var_export($statics, 1)); } // ÐÑзов меÑода printf("---> РезÑлÑÑÐ°Ñ Ð²Ñзова в: "); var_dump($method->invoke(NULL)); ?> ]]> </programlisting> </example> <note> <simpara> ÐопÑÑка вÑзова закÑÑÑого, заÑиÑенного или абÑÑÑакÑного меÑода пÑÐ¸Ð²ÐµÐ´ÐµÑ Ðº вÑбÑоÑÑ Ð¸ÑклÑÑÐµÐ½Ð¸Ñ Ð¸Ð· меÑода <function>invoke</function>. </simpara> </note> <note> <simpara> ÐÐ»Ñ ÑÑаÑиÑеÑÐºÐ¸Ñ Ð¼ÐµÑодов, опиÑаннÑÑ Ð²ÑÑе, Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ Ð¿ÐµÑедаÑÑ NULL в каÑеÑÑве пеÑвого аÑгÑменÑа в <function>invoke</function>. ÐÐ»Ñ Ð½ÐµÑÑаÑиÑеÑÐºÐ¸Ñ Ð¼ÐµÑодов нÑжно пеÑедаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionproperty"> <title><classname>ReflectionProperty</classname></title> <para> ÐлаÑÑ <classname>ReflectionProperty</classname> позволÑÐµÑ Ð¿ÐµÑепÑоекÑиÑоваÑÑ ÑвойÑÑва клаÑÑа. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionProperty implements Reflector { final private __clone() public void __construct(mixed class, string name) public string __toString() public static string export(mixed class, string name, bool return) public string getName() public bool isPublic() public bool isPrivate() public bool isProtected() public bool isStatic() public bool isDefault() public int getModifiers() public mixed getValue(stdclass object) public void setValue(stdclass object, mixed value) public ReflectionClass getDeclaringClass() public string getDocComment() } ?> ]]> </programlisting> </informalexample> <note> <simpara> <function>getDocComment</function> бÑл добавлен в PHP 5.1.0. </simpara> </note> <para> ÐнализиÑÑÑ ÑвойÑÑво, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionProperty</classname>. ÐаÑем, Ð²Ñ ÑможеÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе, в ÑÑом обÑекÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionProperty</classname></title> <programlisting role='php'> <![CDATA[ <?php class String { public $length = 5; } // Создание ÑкземплÑÑа клаÑÑа ReflectionProperty $prop = new ReflectionProperty('String', 'length'); // Print out basic information printf( "===> %s%s%s%s ÑвойÑÑво '%s' (коÑоÑое бÑло %s)\n" . " Ð¸Ð¼ÐµÐµÑ Ð¼Ð¾Ð´Ð¸ÑикаÑоÑÑ %s\n", $prop->isPublic() ? ' public' : '', $prop->isPrivate() ? ' private' : '', $prop->isProtected() ? ' protected' : '', $prop->isStatic() ? ' static' : '', $prop->getName(), $prop->isDefault() ? 'обÑÑвлено во вÑÐµÐ¼Ñ ÐºÐ¾Ð¼Ð¿Ð¸Ð»ÑÑии' : 'Ñоздано во вÑÐµÐ¼Ñ Ð²ÑполнениÑ', var_export(Reflection::getModifierNames($prop->getModifiers()), 1) ); // Создание ÑкземплÑÑа String $obj= new String(); // ÐолÑÑение ÑекÑÑего знаÑÐµÐ½Ð¸Ñ printf("---> ÐнаÑение: "); var_dump($prop->getValue($obj)); // Ðзменение знаÑÐµÐ½Ð¸Ñ $prop->setValue($obj, 10); printf("---> УÑÑановка знаÑÐµÐ½Ð¸Ñ 10, новое знаÑение Ñавно: "); var_dump($prop->getValue($obj)); // Ðамп обÑекÑа var_dump($obj); ?> ]]> </programlisting> </example> <note> <simpara> ÐопÑÑка вÑзова закÑÑÑого или заÑиÑенного ÑвойÑÑва клаÑÑа пÑÐ¸Ð²ÐµÐ´ÐµÑ Ðº вÑбÑоÑÑ Ð¸ÑклÑÑениÑ. </simpara> </note> </sect2> <sect2 xml:id="language.oop5.reflection.reflectionextension"> <title><classname>ReflectionExtension</classname></title> <para> ÐлаÑÑ <classname>ReflectionExtension</classname> позволÑÐµÑ Ð²Ð°Ð¼ пеÑепÑоекÑиÑоваÑÑ ÑаÑÑиÑениÑ. ÐÑ Ð¼Ð¾Ð¶ÐµÑе воÑÑÑановиÑÑ Ð²Ñе загÑÑженнÑе ÑаÑÑиÑÐµÐ½Ð¸Ñ Ð²Ð¾ вÑÐµÐ¼Ñ Ð²ÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ Ð¸ÑполÑзÑÑ <function>get_loaded_extensions</function>. </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php class ReflectionExtension implements Reflector { final private __clone() public void __construct(string name) public string __toString() public static string export(string name, bool return) public string getName() public string getVersion() public ReflectionFunction[] getFunctions() public array getConstants() public array getINIEntries() public ReflectionClass[] getClasses() public array getClassNames() public string info() } ?> ]]> </programlisting> </informalexample> <para> ÐнализиÑÑÑ ÑаÑÑиÑение, ÑнаÑала Ð²Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÑкземплÑÑ ÐºÐ»Ð°ÑÑа <classname>ReflectionExtension</classname>. ÐаÑем, Ð²Ñ ÑможеÑе вÑзваÑÑ Ð»Ñбой из меÑодов, опиÑаннÑÑ Ð²ÑÑе, в ÑÑом обÑекÑе. </para> <example> <title>ÐÑполÑзование клаÑÑа <classname>ReflectionExtension</classname></title> <programlisting role='php'> <![CDATA[ <?php // Создание ÑкземплÑÑа клаÑÑа ReflectionProperty $ext = new ReflectionExtension('standard'); // ÐÑвод оÑновной инÑоÑмаÑии printf( "ÐÐ¼Ñ : %s\n" . "ÐеÑÑÐ¸Ñ : %s\n" . "ФÑнкÑии : [%d] %s\n" . "ÐонÑÑанÑÑ : [%d] %s\n" . "ÐиÑекÑÐ¸Ð²Ñ INI : [%d] %s\n" . "ÐлаÑÑÑ : [%d] %s\n", $ext->getName(), $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', sizeof($ext->getFunctions()), var_export($ext->getFunctions(), 1), sizeof($ext->getConstants()), var_export($ext->getConstants(), 1), sizeof($ext->getINIEntries()), var_export($ext->getINIEntries(), 1), sizeof($ext->getClassNames()), var_export($ext->getClassNames(), 1) ); ?> ]]> </programlisting> </example> </sect2> <sect2 xml:id="language.oop5.reflection.extending"> <title>РаÑÑиÑение клаÑÑов ÑеÑлекÑии</title> <para> Ð ÑлÑÑае, когда Ð²Ñ Ð·Ð°Ñ Ð¾ÑиÑе ÑоздаÑÑ ÑпеÑиализиÑованнÑе веÑÑии вÑÑÑоеннÑÑ ÐºÐ»Ð°ÑÑов (напÑимеÑ, Ð´Ð»Ñ ÑÐ¾Ð·Ð´Ð°Ð½Ð¸Ñ Ð¿Ð¾Ð´ÑвеÑки HTML пÑи ÑкÑпоÑÑе Ñдобно имеÑÑ Ð»ÐµÐ³ÐºÐ¾Ð´Ð¾ÑÑÑпнÑе пеÑеменнÑе взамен меÑодов, либо ÑÑилиÑÑ Ð¼ÐµÑодов), Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе пойÑи далÑÑе и ÑаÑÑиÑиÑÑ Ð¸Ñ . </para> <example> <title>РаÑÑиÑение вÑÑÑоеннÑÑ ÐºÐ»Ð°ÑÑов</title> <programlisting role='php'> <![CDATA[ <?php /** * Свой клаÑÑ Reflection_Method */ class My_Reflection_Method extends ReflectionMethod { public $visibility = array(); public function __construct($o, $m) { parent::__construct($o, $m); $this->visibility = Reflection::getModifierNames($this->getModifiers()); } } /** * Ðемо-клаÑÑ #1 * */ class T { protected function x() {} } /** * Ðемо-клаÑÑ #2 * */ class U extends T { function x() {} } // ÐÑвод инÑоÑмаÑии var_dump(new My_Reflection_Method('U', 'x')); ?> ]]> </programlisting> </example> <note> <simpara> Ðнимание! ÐÑли Ð²Ñ Ð¿ÐµÑепиÑÑваеÑе конÑÑÑÑкÑоÑ, Ñо не забÑдÑÑе вÑзÑваÑÑ ÐºÐ¾Ð½ÑÑÑÑкÑÐ¾Ñ ÑодиÑÐµÐ»Ñ _пеÑед_ лÑбÑм кодом, коÑоÑÑй Ð²Ñ Ð·Ð°Ñем вÑÑавиÑе. Ð ÑлÑÑае невÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ ÑÑого ÑÑÐ»Ð¾Ð²Ð¸Ñ ÑезÑлÑÑÐ°Ñ Ð±ÑÐ´ÐµÑ ÑледÑÑÑим: <literal> Fatal error: Internal error: Failed to retrieve the reflection object </literal> </simpara> </note> </sect2> </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->