php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75058 Check type on return of typehint arguments passed by reference.
Submitted: 2017-08-10 13:27 UTC Modified: 2017-08-10 18:05 UTC
From: email at davekok dot nl Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Next Minor Version OS: any
Private report: No CVE-ID: None
 [2017-08-10 13:27 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-10 13:34 UTC] 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:57 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug -Package: PHP Language Specification +Package: *General Issues
 [2017-08-10 13:57 UTC] requinix@php.net
That.
 [2017-08-10 14:06 UTC] 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 18:05 UTC] email at davekok dot nl
I agree that the use of pass by reference arguments is rare. Perhaps it is not worth the time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC