2010-02-03 25 views
18

採取考慮以下PHP 5類:如何與文檔的phpDocumentor在PHP 5類屬性

class SomeClass 
{ 
    //I want to document this property... 
    private $foo; 


    function __construct() 
    { 

    } 

    public function SetFoo($value) 
    { 
     $this->foo = $value; 
    } 

    public function GetFoo() 
    { 
     return $this->foo; 
    } 
} 

phpDocumentor如何將我的文件$ foo的財產?我甚至不知道它需要記錄,但我想知道如果如果需要...

我知道如何文檔SetFoo()和GetFoo(),我只是不確定私人財產(變量?)。

謝謝!

回答

31
/** 
* This is what the variable does. The var line contains the type stored in this variable. 
* @var string 
*/ 
private $foo; 
15

我通常會至少使用@var標記來指示這是變量的類型。

例如:

/** 
* Some blah blah about what this is useful for 
* @var MyClass $foo 
*/ 


這到底發生了由Zend框架進行,例如;看到Zend_Layout(引用)

class Zend_Layout 
{ 
    /** 
    * Placeholder container for layout variables 
    * @var Zend_View_Helper_Placeholder_Container 
    */ 
    protected $_container; 

    /** 
    * Key used to store content from 'default' named response segment 
    * @var string 
    */ 
    protected $_contentKey = 'content'; 


注:@access標籤是非常有用的PHP 4 (當沒有public/protected/private,但我從來沒有使用它時,我記錄代碼用PHP 5編寫:使用可見性關鍵字的代碼是自行記錄的。

+0

@var MyClass $ foo實際上應該是@property MyClass $ foo,根據你如何使用它(在這種情況下@property表明一個魔術變量)。您發佈的Zend示例確實顯示了@var – 2011-07-13 16:45:03

+0

的正確用法感謝有關@access的詳細信息。這就是我在找到這個頁面時正在尋找的東西。 – Matt 2012-06-16 21:21:10

+0

+1 - 但是應該在第一個例子中的變量名稱中加入變量的名字? – 2012-10-17 16:21:00

0

在您使用__get和__set魔術方法的情況下,你可以使用@property

/** 
    * Description for the class 
    * @property type $foo Description for foo 
    * @property type $foo Description for bar 
    */ 
class SomeClass 
{ 
    private $foo; 
    protected $bar; 

    public function __get(){ 
     ... 
    } 

    public function __set(){ 
     ... 
    } 
} 

鏈接更多的信息:

+3

'@ property'用於標記魔術屬性。要標記類成員,請使用'@ var'。請參閱http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.property.pkg。html – DanFromGermany 2014-12-11 16:07:32

+0

在你的例子中,'$ foo'和'$ bar'沒有神奇的屬性,因爲你聲明瞭它們。 – DanFromGermany 2014-12-11 16:09:06

1
/** 
* docstring 
*/ 
private $foo; 

重要說明:應該有兩個星號。不是一個。