Req #60432 [Com]: Improving typehinting in function args
| From: | 6562680 at gmail dot com | Date: | Mon, 09 Sep 2019 10:27:32 +0000 |
| Subject: | Req #60432 [Com]: Improving typehinting in function args | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222647@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60432&edit=1
ID: 60432
Comment by: 6562680 at gmail dot com
Reported by: duke at ejoom dot com
Summary: Improving typehinting in function args
Status: Closed
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: *
PHP Version: >=5.5
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Hope we got
register_typehint($name, \Closure $func) in future
really tired of write something like this:
function (array $dict)
{
if (! Lib::is_dict($dict))
throw new \InvalidArgumentException('Argument 1 should be dict');
}
Previous Comments:
------------------------------------------------------------------------
[2015-03-21 20:33:36] nikic@php.net
Scalar typehints have been implemented in PHP 7, though with different semantics than described
here, see https://wiki.php.net/rfc/scalar_type_hints_v5.
------------------------------------------------------------------------
[2013-06-26 07:35:59] duke at ejoom dot com
How can I add it to RFC. I registered in wiki but I had not access to create page https://wiki.php.net/rfc/improve-parameter-hint
.
------------------------------------------------------------------------
[2013-06-23 08:44:39] knight at kopernet dot org
It's an interesting proposal to make typehinting more configurable.
I'd vote for something in completely opposite direction.
I consider the current behavior of PHP very unreasonable.
I would really love if the typehints emitted just warning instead of the fatal error that's
generated right now. It makes adoption of duck types in for example unit tests much more difficult
and when the signature uses final keyword as well it's impossible to create a mock using
neither PHP Unit Mocks nor Mockery.
PHP is a dynamic language.
------------------------------------------------------------------------
[2012-03-05 08:51:58] daanleduc at hotmail dot com
Would be also nice if its possible to add an more strict mode for functions.
For example ->
function test(int $int, string $string, float $float) {}
behavior ->
function('test', 12, "blaat") {} <-- shouldn't throw
ini_set('function_strict', true);
function('test', 12, "blaat") {} <-- should throw
It should be really strict so if its not an int it is not valid (triple ===
check). Even if its an int string ("1").
What duke suggest is nice but still breaks if you do an bool (true) in an int,
its casts safely to int 1. Should just throw if you put the strict mode on or
maybe build some levels of strict mode.
It would save me and a lot of other developers all the sanity checks.
------------------------------------------------------------------------
[2011-12-02 21:26:12] duke at ejoom dot com
It can be possible to use the function to delete rows in DB:
function delete( array $ids ) { //some code}
delete(3); //will work correct as expected.
// Or
delete(array(3,6));// will work.
Or we would be able to use that behaviour:
$object->property1 = 1;
$object->property2 = 'value';
tipehind($object);
//where $object converting into array.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=60432
--
Edit this bug report at https://bugs.php.net/bug.php?id=60432&edit=1