php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60432 Improving typehinting in function args
Submitted: 2011-12-02 21:12 UTC Modified: 2015-03-21 20:33 UTC
Votes:7
Avg. Score:3.7 ± 1.4
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:4 (100.0%)
From: duke at ejoom dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: >=5.5 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: duke at ejoom dot com
New email:
PHP Version: OS:

 

 [2011-12-02 21:12 UTC] duke at ejoom dot com
Description:
------------
I want to discuss an improving typehint.

I propose to do typehint behaviour as following:
On the Object instance it will behave as current (emit an error).
At the array int and float it must do equivalence:

function typehint( array $array, string $string, int $int, float $float )
{}
   will be equivalent to
function typehint( $array, $string, $int, $float )
{
	if (is_array($array) === false) $array = (array) $array;
	$string = (string) $string;
	$int = (int) $int;
	$float = (float) $float;
}

It will do code more readable and compact. For Zend Framework DB it will do easier typing query where escaping is based on type of var,

Isn't logical emit error on mismatching of type on int and string at PHP caused by easing change type against C or C++. So we must use it feature of PHP and do life easier ;).


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-02 21:26 UTC] 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.
 [2012-03-05 08:51 UTC] 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.
 [2013-06-23 08:44 UTC] 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.
 [2013-06-26 07:35 UTC] duke at ejoom dot com
-PHP Version: 5.4.0RC2 +PHP Version: >=5.5
 [2013-06-26 07:35 UTC] 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 .
 [2015-03-21 20:33 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2015-03-21 20:33 UTC] 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.
 [2019-09-09 10:27 UTC] 6562680 at gmail dot com
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');
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 23:01:33 2024 UTC