作为一名 Emacs 用户,我唯一羡慕像 PDT 这样的“现代”编辑器的是 PDT 理解类型,甚至 PHPDoc“类型提示”,例如,
/**
* @param DateTime $date
* @param string $name
* @return DOMDocument
*/
目前,我尽可能使用类型提示,以使 PHP 解释器在我这样做时提醒我。参数类型错误,但只有当参数是对象时才有效。更重要的是,无法确保函数的返回值是特定类型。
这可能是一个漫长的过程,但是有没有任何插件或其他方式可以让 PHP 解释器像 PDT 一样了解 PHPDoc 注释?理想情况下,如果我返回错误类型的值,或者传递一个已将参数声明为 int 的字符串,我希望得到致命错误。
As an Emacs user, the only thing that I envy about "modern" editors like PDT is that PDT understands types, and even PHPDoc "type hints", e.g.
/**
* @param DateTime $date
* @param string $name
* @return DOMDocument
*/
Currently I use type hints wherever I can to make the PHP interpreter alert me if I get the parameter type wrong, but that only works if the parameter is an object. More importantly, there's no way to ensure that a function's return value is of a specific type.
This might be a long shot, but is there any plugin or other way of making the PHP interpreter aware of PHPDoc comments in the same way that PDT is? Ideally, I would like to get fatal errors if I return a value of the wrong type, or if I pass a string where I have declared the parameter as an int, for example.
发布评论
评论(3)
您应该查看 SplTypes 扩展(注意:它是实验性的)。这允许“基元”的类型提示(最后我听说它们在所有可以想象的方面都比基元更好)。
你不能以任何其他方式让解释器强制原语,尽管有一些难以想象的烦人的解决方法,我将把它们作为练习留给读者。
You should look into the SplTypes extension (caution: it is experimental). That allows type hinting of "primitives" (last I heard they are better than primitives in every way imaginable).
You can't make the interpreter force primitives in any other way, though there are unimaginably annoying workaround which I will leave as an exercise for the reader.
不。 PHP 不支持显式类型声明或类型提示,使用 类型杂耍 明确是 PHP 解释器的一部分。根据类型提示的文档,仅支持对象和数组类型提示。
也就是说,如果您的函数具有严格的类型要求,则必须在函数开始时验证这些参数。如果您非常喜欢 PHP 中的类型概念,您可以 a) 切换到类型化语言(呵呵),或者 b) 使用自动装箱/对象包装模式。这会带来显着的性能损失,但您可以创建包装类并使用类型提示,而不是使用原始类型检查(即
is_string
):如您所见,这是一座相当大的山攀爬(为每种类型创建一个包装器)只是为了避免:
我的结论是:不值得。
您可以在此处阅读有关自动装箱的更多信息: http://php.webtutor.pl/en/2011/04/13/strong-data-typing-in-php-part-ii-autoboxing-and-indestructable-objects-english-version/
另外,请注意此用于自动装箱的 RFC:https://wiki.php.net/rfc/autoboxing不过它已经存在了一段时间了,所以我不会坚持下去。
No. PHP does not support explicit type declaration or type hinting, use of type juggling is explicitly part of the PHP interpreter. Per the documents on type hinting, only object and array type hints are supported.
That said, if you have a function with tight type requirements, it becomes essential to validate those arguments at the start of a function. If you are very attached to the idea of types in PHP, you can either a) switch to a typed language (heh), or b) use an autoboxing/object-wrapper pattern. There is a significant performance penalty to this, but instead of using primitive checks for type (ie
is_string
), you can create the wrapper class and use type hinting:As you can see, that's a pretty big hill to climb (with creating a wrapper for each type) just to avoid:
My verdict is: not worth it.
You can read some more on autoboxing here: http://php.webtutor.pl/en/2011/04/13/strong-data-typing-in-php-part-ii-autoboxing-and-indestructable-objects-english-version/
Also, note this RFC for autoboxing: https://wiki.php.net/rfc/autoboxing It has been around for a while, though, so I wouldn't hold out for it.
关于如何为标量类型进行类型提示,这是一场大讨论:
但至于我现在知道开发人员解决方案根本不使用标量类型提示
PHP 手册 说:
类型提示只能是对象和数组(自 PHP 5.1 起)类型。不支持使用 int 和 string 的传统类型提示。
It was big discussion how to make type hinting for scalar types:
But As far as I know now developers solution to not use scalar type hinting at all
PHP manual says:
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.