2013-04-22 53 views
0
<?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'; 
} 
?> 

問題:試圖瞭解ReflectionClass在PHP

  1. var_export($class->getParentClass(), 1),輸出爲: ===>的用戶定義的類 '計數器'[延伸ReflectionClass :: __ set_state(陣列(「名'=>'Object',))] ... var_export($class->getParentClass()),output is:ReflectionClass :: __ set_state(array('name'=>'Object',))===>用戶定義的類'Counter'[extends ] ...爲什麼?這個意思是:'__set_state'?這是什麼意思?__set_state是什麼意思?__set_state是什麼意思?__set_state是什麼意思?__set_state是什麼意思?__set_state是什麼意思?__set_state是什麼意思?

  2. getModifiers()getModifierNames()這兩個函數實際上是什麼意思?

+2

請格式化您的代碼。 – Sampson 2013-04-22 02:02:48

+1

...和你的問題 – hek2mgl 2013-04-22 02:03:20

回答

1

答案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'); 

,以測試它

+0

'getModifiers()'有一個小錯字。請檢查我的編輯 – hek2mgl 2013-04-22 03:56:21