哪个PHP文档注释变量类型声明是正确的?

时间:2016-12-10 16:04:58

标签: php documentation

我搜索了不同的指南&文档如何正确地在文档中声明变量类型,但似乎有几种不同的选项。

有三种不同的可能性:

/** @var integer $sum */
/** @var $sum integer */
/* any of the above but with 1 asterisk */

对于方法参数和变量非常清楚如何写,因为它是在很多不同的文档中编写的。但是,如果我们讨论的不是函数或View文件中的变量(MVC设计模式)呢?

在我的工作场所,我们目前使用2种不同的软件( NetBeans PhpStorm )。我们注意到它们提供了不同的模板来在文档中声明变量类型。

在PhpStorm中:

enter image description here

在NetBeans中:

enter image description here

在StackOverflow中,我还发现了两种不同的用途(highly upvoted answer uses NetBeans option with 1 asteriskin another question it says it's reversed and also uses 2 asterisks)。

由于我们使用的是Yii2框架,我们还看了他们如何撰写文档评论。他们正在使用this format

/* @var $this yii\web\View */

我感兴趣的最后一件事(不像上面的问题那么重要)是我在评论中声明对象时是否需要在注释或用法中声明完整路径?完整路径它想:

/** @var yii\BaseYii $object */
echo $object::createObject(1);

完整路径看起来像这样:

use yii\BaseYii;

/** @var BaseYii $object */
echo $object::createObject(1);

我们希望尽可能准确地遵循PHP标准。

感谢任何帮助。

我知道这个问题可能太宽泛不清楚你在问什么,我也会仔细观察它是怎么回事。

1 个答案:

答案 0 :(得分:2)

我常见的是PSR-5建议:

namespace \A\B\C;

use yii\BaseYii;

/** @var BaseYii $yii */
$yii = $factory->getApplication();

如果声明完整路径,那么这些都是有效的例子:

namespace \A\B\C;

/** @var \yii\BaseYii $yii */
$yii = $factory->getApplication();

namespace \A\B\C;

/** @var \ArrayObject|null $array */
$array = null;

/**
 * @return \DateTime
 */
function now()
{

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="foo")
 */
class Foo

app.use(function(req,res,next) {
    if(req.headers["x-forwarded-proto"] == "http") {
        res.redirect("https://[your url goes here]" + req.url, next);
    } else {
        return next();
    } 
});