Bug #81144 [Opn->Nab]: Cuckoo egg
| From: | cmb@php.net | Date: | Wed, 16 Jun 2021 09:35:14 +0000 |
| Subject: | Bug #81144 [Opn->Nab]: Cuckoo egg | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-234427@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81144&edit=1
ID: 81144
Updated by: cmb@php.net
Reported by: wojtek at uniwizard dot com
Summary: Cuckoo egg
-Status: Open
+Status: Not a bug
Type: Bug
Package: Variables related
Operating System: Alpine Linux 3.13
PHP Version: 7.4.20
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[2021-06-16 09:23:47] 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"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81144&edit=1