Bug #75058 [Nab]: Check type on return of typehint arguments passed by reference.
| From: | email at davekok dot nl | Date: | Thu, 10 Aug 2017 18:05:34 +0000 |
| Subject: | Bug #75058 [Nab]: Check type on return of typehint arguments passed by reference. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210587@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75058&edit=1
ID: 75058
User updated by: email at davekok dot nl
Reported by: email at davekok dot nl
Summary: Check type on return of typehint arguments passed by
reference.
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: any
PHP Version: Next Minor Version
Block user comment: N
Private report: N
New Comment:
I agree that the use of pass by reference arguments is rare. Perhaps it is not worth the time.
Previous Comments:
------------------------------------------------------------------------
[2017-08-10 14:06:50] spam2 at rhsoft dot net
"This is not a bug. Parameters are checked on input and shouldn't be used for output"
is not correct - parameters are checked by the caller itself and in non-strict-mode typecasted and
in strict-mode the caller trows an exception
"A error is thrown on foo's return stating that the variable $i has an incorrect
type" is in that samples just plain wrong because they don't return anything, one of the
millions reasons why you should *not* use references at all, if you *really* return something you
can enforce types
http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html
_____________________________
function foo(int &$i)
{
$i = "string"; // incorrect type
}
there is nothing incorrect because you can do whatever you want with a variable until type-hints (at
call a function) or return-types are part of the game
function foo(int &$i): int
{
$i = "string";
return (int)$i;
}
the sample above would be 100% valid
* if it is a reference don't matter
* $i is checked by the caller and casted or lead ot a exception at the caller
* the return value is still correct because if the type-casting at return (int)
------------------------------------------------------------------------
[2017-08-10 13:57:42] requinix@php.net
That.
------------------------------------------------------------------------
[2017-08-10 13:34:06] kelunik@php.net
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.
------------------------------------------------------------------------
[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