简体   繁体   English

PHP 类型提示 - 代码与注释

[英]PHP type hinting - code vs. annotations

PHP 5 can do some (limited) type hinting , however, it seems to me that in real-world projects, types are usually described in doc comments. PHP 5 可以做一些(有限的)类型提示,但是,在我看来,在实际项目中,类型通常在文档注释中描述。 For example, instead of this:例如,而不是这样:

/**
 * Test method
 */
function test(SomeType $param1) {
    ...
}

there will more commonly be更常见的是

/**
 * Test method
 *
 * @param SomeType param1
 */
function test($param1) {
    ...
}

What are the advantages and disadvantages of the two approaches?这两种方法的优缺点是什么? And if I'm right in the assumption that the PhpDoc method is more common, why is that?如果我认为 PhpDoc 方法更常见是正确的,为什么会这样? Why don't people utilize the built-in language feature more?为什么人们不更多地利用内置语言功能?

Edit : the third option is to use both approaches combined:编辑:第三个选项是结合使用两种方法:

/**
 * Test method
 *
 * @param SomeType param1
 */
function test(SomeType $param1) {
    ...
}

However, I personally haven't seen this used too often (looked at libraries like Symfony or PHPUnit) and this also seems like doing some work for not much additional benefit to be honest.但是,我个人还没有看到它经常使用(查看了 Symfony 或 PHPUnit 之类的库),而且老实说,这似乎也做了一些工作,但没有太多额外的好处。 Maybe that's why it isn't seen more often.也许这就是为什么它不经常出现的原因。

First thing: PHP typehints have different ability to hint than PHPDoc.第一件事:PHP 类型提示具有与 PHPDoc 不同的提示能力。 Differences are (at least):差异是(至少):

  • Scalar types.标量类型。 Before PHP 7.1, you can not hint scalar types, while nothing prevents you from hinting在 PHP 7.1 之前,您不能提示标量类型,而没有什么可以阻止您提示

    /** * @param string $param Param description */
  • Array of hinting.提示数组。 In PHPDoc you can hint, that parameter (or return value) is array of something.在 PHPDoc 中,您可以提示,该参数(或返回值)是某个数组。 It would be:这将是:

     /** * @param ClassName[] $param Param description */

    and meaning of this is - array of instances of ClassName .其含义是 - ClassName的实例数组。 This is extremely useful when it comes to return type (because IDE then may do substitutions for methods on iteration of that array, and, therefore, you'll know that you're doing correct thing or not).这在返回类型方面非常有用(因为 IDE 可能会在该数组的迭代中替换方法,因此,您将知道自己是否在做正确的事情)。 However, in PHP you can only typehint it as但是,在 PHP 中,您只能将其键入提示为

    function functionName(array $param) { /*...*/ }

    so it won't be possible to realize what are actual elements of array.所以不可能意识到数组的实际元素是什么。 For your information there is a corresponding RFC for typehinting as array of some elements, which is currently rejected - but may be in future such possibility will appear in PHP.对于您的信息,有一个相应的RFC用于将类型提示作为某些元素的数组,目前被拒绝 - 但将来这种可能性可能会出现在 PHP 中。

But, on the other hand, using PHP typehints is still different thing and normally you should do both - so, if it's possible to hint somehow in PHP (like for array sample above) - do it, and add PHPDoc block as well.但是,另一方面,使用 PHP typehints 仍然是不同的事情,通常你应该两者都做 - 所以,如果可以在 PHP 中以某种方式提示(如上面的数组示例) - 这样做,并添加 PHPDoc 块。 PHP typehint allows you to enforce behavior on language level, while PHPDoc is only "information" source, it serves only information goal and can not prevent of passing illegal argument type. PHP typehint 允许您在语言级别强制执行行为,而 PHPDoc 只是“信息”源,它仅服务于信息目标,不能防止传递非法参数类型。

Personally I would use both.我个人会同时使用两者。

First option is good for controling what objects are you passing to the method.第一个选项有利于控制传递给方法的对象。 Second usually can be added automaticaly by any modern IDE and it makes your code more readable. Second 通常可以由任何现代 IDE 自动添加,它使您的代码更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM