Req #75058 [Com]: Check type on return of typehint arguments passed by reference.

[email protected] ("[email protected]") Thu, 10 Aug 2017 13:34:08 GMT
Newsgroups php.standards
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=75058&edit=1

 ID:                 75058
 Comment by:         [email protected]
 Reported by:        email at davekok dot nl
 Summary:            Check type on return of typehint arguments passed by
                     reference.
 Status:             Open
 Type:               Feature/Change Request
 Package:            PHP Language Specification
 Operating System:   any
 PHP Version:        Next Minor Version
 Block user comment: N
 Private report:     N

 New Comment:

This is not a bug. Parameters are checked on input and shouldn't be used for output.

The thing you want are typed variables, which do not exist, because it would have to be checked on every assignment, not only on return.


Previous Comments:
------------------------------------------------------------------------
[2017-08-10 13:27:31] email at davekok dot nl

Description:
------------
When declaring a function with a type hinted argument passed by reference, the argument's type is not checked on the function's return. I would expect that when calling a function with a type hinted argument passed by reference. That the variable used will still contain data of that type when the function finishes.

Test script:
---------------
<?php

function foo(int &$i)
{
   $i = "string"; // incorrect type
}

function bar(?int &$i)
{
   $i = null; // correct type
}

function baz(?int &$i)
{
   $i = 4; // correct type
}


Expected result:
----------------
A error is thrown on foo's return stating that the variable $i has an incorrect type.

Actual result:
--------------
The code continues as if all is well.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=75058&edit=1