php.net |  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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wojtek at uniwizard dot com
New email:
PHP Version: OS:

 

 [2021-06-16 09:23 UTC] wojtek at uniwizard dot com
Description:
------------
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:
---------------
<?php
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()"]=>
  int(234)
}
array(1) {
  ["result 2 from inside test_function()"]=>
  string(10) "cuckoo egg"
}
array(1) {
  ["result 3 from outside test_function()"]=>
  string(10) "cuckoo egg"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-16 09:35 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-06-16 09:35 UTC] cmb@php.net
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] <https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.misc>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 08:01:28 2024 UTC