Hi guys,
I'm a frequent lurker on the PHP internals mailing list, so a few days ago I witnessed
the beginning of a "hot" discussion. The issue is about introducing typed parameters
in PHP. Ilia Alshanetsky brought a third patch (in the past there were other two
attempts made by other developers) that introduces a new syntax for checking
types of parameters. It looked like this:
Note! There are no types for return values.PHP Code:<?php
function foo(int $a, string $b) {}
However, people raise the problem of dynamic typing which is one of the key
features in PHP, and proposed that this feature be accompanied by a way of
casting arguments right there, in the parameters' body. There where three
long threads, but a few days ago there was a call for vote for a final patch
that introduces the following:
Strict type check
Weak type check/hint/castPHP Code:<?php
/**
* Whenever the type of $a is not integer, an E_RECOVERABLE_ERROR
* is issued.
*/
function foo(int $a, string $b) {}
This new feature was proposed for addition to the language in the currentPHP Code:<?php
/**
* This solution uses the existent syntax of type casting. Whenever the
* argument cannot be casted to the desired type, an E_RECOVERABLE_ERROR with
* an appropriate message is issued.
*/
function foo((int) $a, (string) $b) {}
PHP 5.3 branch.
Many people voted for it, but I guess they missed the PHP 5.3 part because,
after someone said it is against it for PHP 5.3, but agrees for a future version
like 5.4 or 6, many others changed their minds in the same way.
Others raised the issue of backwards compatibility because of existing
classes with names such as object, string, etc.
So, what do you guys think about this? I know many of you wanted
something like this (including types for return values).
Personally, I like the type hinting part a lot. The strict type checking is
something that I don't really love as this will produce lots of code for type
casting before a function call, instead of having it in the function. For
example:
Whereas the parameter type hinting solves this problem in one place only,PHP Code:<?php
function foo(int $a, string $b) {}
foo((int) '1');
foo((int) '2');
foo((int) '3');
foo((int) '4');
generating the same error whenever a certain variable cannot be casted to
the desired type.









). 
Bookmarks