繁体   English   中英

试图了解PHP中的ReflectionClass

[英]trying to understand ReflectionClass in php

<?php
interface NSerializable
{
// ...
}
class Object
{
// test...
}
/**
* A counter class test
*/
class Counter extends Object implements NSerializable
{
const START = 0;
private static $c = Counter::START;
/**
* Invoke counter test
*
* @access public
* @return int
*/
public function count()
{
return self::$c++;
}
}
// Create an instance of the ReflectionClass class
$class = new ReflectionClass('Counter');
// Print out basic information
printf(
"===> The %s%s%s %s '%s' [extends %s]\n" .
" declared in %s\n" .
" lines %d to %d\n" .
" having the modifiers %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()))
);
// Print documentation comment
printf("---> Documentation:\n %s\n",
var_export($class->getDocComment(), 1));
// Print which interfaces are implemented by this class
printf("---> Implements:\n %s\n",
var_export($class->getInterfaces(), 1));
// Print class constants
printf("---> Constants: %s\n",
var_export($class->getConstants(), 1));
// Print class properties
printf("---> Properties: %s\n",
var_export($class->getProperties(), 1));
// Print class methods
printf("---> Methods: %s\n",
var_export($class->getMethods(), 1));
// If this class is instantiable, create an instance
if ($class->isInstantiable())
{
$counter = $class->newInstance();
echo '---> $counter is instance? ';
echo $class->isInstance($counter) ? 'yes' : 'no';
echo "\n---> new Object() is instance? ";
echo $class->isInstance(new Object()) ? 'yes' : 'no';
}
?>

问题:

  1. var_export($class->getParentClass(), 1) ,输出为:===>用户定义的类'Counter'[扩展ReflectionClass :: __ set_state(array('name'=>'Object',)))]。 .. var_export($class->getParentClass()) ,输出为:ReflectionClass :: __ set_state(array('name'=>'Object',))===>用户定义的类'Counter'[extends]。 ..为什么?

  2. $class->getParentClass() ,输出是:ReflectionClass :: __ set_state(array('name'=>'Object',))这是什么意思:'__set_state'?

  3. getModifiers()getModifierNames()这两个函数实际上是什么意思?

答案1和2:

$class->getParentClass()实际上是类型为ReflectionClass的对象,而不是字符串。

更换:

var_export($class->getParentClass())

通过:

$class->getParentClass()->getName()

3。 没有方法ReflectionClass::getModifierNames() 我不知道您从何处获得此信息,但如果需要,则必须编写自己的信息。 这是扩展ReflectionClass并添加方法的示例:

class MyReflectionClass extends ReflectionClass 
{

    /**
     * Returns the modifiers in human readable format
     */
    public function getModifierNames() {
        $m = $this->getModifiers();
        $names = array();
        if($m & self::IS_EXPLICIT_ABSTRACT === self::IS_EXPLICIT_ABSTRACT
        || $m & self::IS_IMPLICIT_ABSTRACT === self::IS_IMPLICIT_ABSTRACT) {
            $names []= 'abstract';
        }
        if($m & self::IS_FINAL === self::IS_FINAL) {
            $names []= 'final';
        }

        return implode(' ', $names);
    }

}

采用:

abstract class Counter extends Object 
  implements NSerializable
{ // ...

要么

final class Counter extends Object 
  implements NSerializable
{ // ...

$class = new MyReflectionClass('Counter');

测试一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM