doupo2157 2013-04-22 02:00
浏览 58
已采纳

试图理解php中的ReflectionClass

<?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]
" .
" declared in %s
" .
" lines %d to %d
" .
" having the modifiers %d [%s]
",
$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:
 %s
",
var_export($class->getDocComment(), 1));
// Print which interfaces are implemented by this class
printf("---> Implements:
 %s
",
var_export($class->getInterfaces(), 1));
// Print class constants
printf("---> Constants: %s
",
var_export($class->getConstants(), 1));
// Print class properties
printf("---> Properties: %s
",
var_export($class->getProperties(), 1));
// Print class methods
printf("---> Methods: %s
",
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 "
---> new Object() is instance? ";
echo $class->isInstance(new Object()) ? 'yes' : 'no';
}
?>

Questions:

  1. var_export($class->getParentClass(), 1), output is: ===> The user-defined class 'Counter' [extends ReflectionClass::__set_state(array( 'name' => 'Object', ))]... var_export($class->getParentClass()), output is: ReflectionClass::__set_state(array( 'name' => 'Object', ))===> The user-defined class 'Counter' [extends ]... why?

  2. $class->getParentClass(), output is: ReflectionClass::__set_state(array( 'name' => 'Object', )) what does this mean: '__set_state'?

  3. getModifiers(),getModifierNames() what does these two functions actually mean?

  • 写回答

1条回答 默认 最新

  • donglu1472 2013-04-22 02:11
    关注

    Answers 1. and 2.:

    $class->getParentClass() is actually an object of type ReflectionClass not a string.

    Replace:

    var_export($class->getParentClass())
    

    by:

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

    3 . There is not method ReflectionClass::getModifierNames(). I don't know where you got this information from, but if you need it you'll have to write your own. Here comes an example that extends ReflectionClass and adds the method:

    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);
        }
    
    }
    

    Use:

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

    or

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

    and

    $class = new MyReflectionClass('Counter');
    

    to test it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测