2011-06-06 77 views

回答

12

嘗試:

<?php 
class Expense { 

    /** 
    * @var int 
    */ 
    private $id; 
} 

$refClass = new ReflectionClass('Expense'); 
foreach ($refClass->getProperties() as $refProperty) { 
    if (preg_match('/@var\s+([^\s]+)/', $refProperty->getDocComment(), $matches)) { 
     list(, $type) = $matches; 
     var_dump($type); 
    } 
} 

輸出

string(3) "int" 
+0

簡明輝煌的代碼 – user1502826 2015-10-01 14:09:11

2

獲取完整的文檔塊:

$reflection = new ReflectionProperty('Expense', 'id'); 

$doc = $reflection->getDocComment(); 
0

警告的一點 - PHP加速器和一些庫本身(即symfony core)評論,通常在運行。