News for hackers. Stuff that matters.
Re:PHP7 Gains Scalar Type Hints by TazeTSchnitzel on Monday March 16, 2015 @07:41PM (#9215284)
PHP has had type hints for classes for more than a decade now:

  function foobar(MyClass $foo) {
     // do stuff
  }
  foobar(new NotMyClass()); // throws an error
This just adds type hints for the scalar types (integer, float, string and boolean).

Since PHP has a long tradition of weak typing and this is what PHP's built-in and extension functions use, weak is the default behaviour, allowing some conversions:

  function foo(int $x) {
     var_dump($x);
  }
  foo("12"); // produces int(12)
  foo(null); // throws an error
But, you can optionally turn on a strict type-checking mode for scalars on a per-file basis, which doesn't allow conversions:

  <?php
  declare(strict_types=1);

  function foo(int $x) {
     var_dump($x);
  }
  foo("12"); // throws an error
  foo(null); // throws an error

There are no comments on this comment yet.