Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the PHP interpreter be made aware of PHPDoc type hints?

Tags:

php

phpdoc

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.

like image 680
Andreas Jansson Avatar asked Aug 16 '11 20:08

Andreas Jansson


2 Answers

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.

like image 173
cwallenpoole Avatar answered Oct 20 '22 00:10

cwallenpoole


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.

like image 36
Chris Baker Avatar answered Oct 20 '22 00:10

Chris Baker