PHP 解释器可以了解 PHPDoc 类型提示吗?

发布于 2024-11-30 01:55:58 字数 392 浏览 1 评论 0 原文

作为一名 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断念 2024-12-07 01:55:58

您应该查看 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.

温柔少女心 2024-12-07 01:55:58

不。 PHP 不支持显式类型声明或类型提示,使用 类型杂耍 明确是 PHP 解释器的一部分。根据类型提示的文档,仅支持对象和数组类型提示。

也就是说,如果您的函数具有严格的类型要求,则必须在函数开始时验证这些参数。如果您非常喜欢 PHP 中的类型概念,您可以 a) 切换到类型化语言(呵呵),或者 b) 使用自动装箱/对象包装模式。这会带来显着的性能损失,但您可以创建包装类并使用类型提示,而不是使用原始类型检查(即 is_string):

class StringWrapper {
  public $value = null;
  function __construct($val) {
    $this->value = $val;
  }

}

function requires_a_string(StringWrapper $string_value) {
 echo $string_value->value;
}

$string = new StringWrapper('Hello world!');

如您所见,这是一座相当大的山攀爬(为每种类型创建一个包装器)只是为了避免:

function requires_a_string($string_value='') {
 if (!is_string($string_value))
  return false;
 echo $string_value;
}

我的结论是:不值得。

您可以在此处阅读有关自动装箱的更多信息: 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:

class StringWrapper {
  public $value = null;
  function __construct($val) {
    $this->value = $val;
  }

}

function requires_a_string(StringWrapper $string_value) {
 echo $string_value->value;
}

$string = new StringWrapper('Hello world!');

As you can see, that's a pretty big hill to climb (with creating a wrapper for each type) just to avoid:

function requires_a_string($string_value='') {
 if (!is_string($string_value))
  return false;
 echo $string_value;
}

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.

天冷不及心凉 2024-12-07 01:55:58

关于如何为标量类型进行类型提示,这是一场大讨论:

  • 强烈检查类型
  • 总是尝试玩弄它
  • 尝试玩弄它,如果 obivivos ie '123' 到 int(123) 但不是 '123aaa' 到 int(123)

但至于我现在知道开发​​人员解决方案根本不使用标量类型提示

PHP 手册 说:
类型提示只能是对象和数组(自 PHP 5.1 起)类型。不支持使用 int 和 string 的传统类型提示。

It was big discussion how to make type hinting for scalar types:

  • Strongly check type
  • Try to juggle it always
  • Try to juggle it if obivios i.e '123' to int(123) but not '123aaa' to int(123)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文