Can a PHP interpreter be aware of hints such as PHPDoc?

As an Emacs user, the only thing I envy for "modern" editors like PDT is that PDT understands types and even hints like "PHPDoc", for example

/** * @param DateTime $date * @param string $name * @return DOMDocument */ 

I am currently using type hints wherever I can to get the PHP interpreter to notify me if I get the parameter type incorrectly, but this only works if the parameter is an object. More importantly, there is no way to guarantee that the return value of a function is of a particular type.

It may be a long shot, but is there any plugin or other way to make the PHP interpreter aware of PHPDoc comments just like PDT? Ideally, I would like to receive fatal errors if I return a value of the wrong type, or if I pass a string where I declared this parameter as int, for example.

+6
source share
3 answers

You should study the SplTypes extension (caution: this is experimental). This allows you to enter types of "primitives" (the last time I heard that they are better than primitives in every way).

You cannot force the power primitives of the interpreter in any other way, although there is an unimaginable annoying workaround that I will leave as an exercise for the reader.

+2
source

Not. PHP does not support explicit type declarations or hint types; using the juggling type is clearly part of the PHP interpreter. In documents in a type, only hints of the type of the object and array are hinted.

However, if you have a function with strict type requirements, it becomes important to check these arguments at the beginning of the function. If you are very attached to the idea of ​​types in PHP, you can: a) switch to a typed language (heh) or b) use the autoboxing / object-wrapper pattern. There is a significant performance is_string for this, but instead of using primitive type checks (i.e. is_string ), you can create a wrapper class and use the hint type:

 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, this is a fairly large hill to raise (with the creation of a wrapper for each type) to avoid:

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

My verdict: not worth it.

Here you can read a few more autoboxins: http://php.webtutor.pl/en/2011/04/13/strong-data-typing-in-php-part-ii-autoboxing-and-indestructable-objects-english- version / Also pay attention to this RFC for autoboxing: https://wiki.php.net/rfc/autoboxing It has been a while, so I would not hold out for that.

+1
source

There was a lot of discussion on how to make a hint type on scalar types:

  • Strongly check type
  • Try to constantly juggle them.
  • Try luring it if obivios ie '123' is in int (123) but not "123aaa" in int (123)

But As far as I know, now developers do not use the scalar type, hinting at everything

The PHP manual says:
The hint type can only be an object and an array type (starting with PHP 5.1). The traditional hinting type with int and string is not supported.

0
source

Source: https://habr.com/ru/post/895190/


All Articles