|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81144 Cuckoo egg
Submitted: 2021-06-16 09:23 UTC Modified: 2021-06-16 09:35 UTC
From: wojtek at uniwizard dot com Assigned: cmb (profile)
Status: Not a bug Package: Variables related
PHP Version: 7.4.20 OS: Alpine Linux 3.13
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.
Block user comment
Status: Assign to:
Bug Type:
From: wojtek at uniwizard dot com
New email:
PHP Version: OS:


 [2021-06-16 09:23 UTC] wojtek at uniwizard dot com
Using the references of function/method arguments of a specific type, I have the option to "throw" a variable of a specific type with a value of a different type than the specified one.
Sample scipt started by simply: "php test.php"

Test script:
function test_function(int &$variableByReference, $value)
    var_dump(['result 1 from inside test_function()' => $variableByReference]);
    $variableByReference = $value;
    var_dump(['result 2 from inside test_function()' => $variableByReference]);

$testVar1 = 234;
$testValue = 'cuckoo egg';
test_function($testVar1, $testValue);
var_dump(['result 3 from outside test_function()' => $testVar1]);

Expected result:
I expect, when set variable "$testValue = 'cuckoo egg';" and start script, to get an error message something like this: "Fatal error: Uncaught TypeError: Argument 1 passed to test_function() must be of the type int, string given".

This means the value of the variable is tested ONLY on SET value of reference, not RETURNED by reference value.

Actual result:
The script set anything to reference without testing return value (by reference).

$ php test.php 
array(1) {
  ["result 1 from inside test_function()"]=>
array(1) {
  ["result 2 from inside test_function()"]=>
  string(10) "cuckoo egg"
array(1) {
  ["result 3 from outside test_function()"]=>
  string(10) "cuckoo egg"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-16 09:35 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-06-16 09:35 UTC]
That is expected behhavior.  From the docs[1]:

| Declared types of reference parameters are checked on function
| entry, but not when the function returns, so after the function
| had returned, the argument's type may have changed.

[1] <>
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Nov 29 00:01:26 2023 UTC