note 124383 added to language.references.pass
| From: | JasonSteelman@php.net | Date: | Tue, 12 Nov 2019 18:13:30 +0000 |
| Subject: | note 124383 added to language.references.pass | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213038@lists.php.net to get a copy of this message | ||
Within a class, passing array elements by reference which don't exist are added to the array as
null. Compared to a normal function, this changes the behavior of the function from throwing an
error to creating a new (null) entry in the referenced array with a new key.
<?php
class foo {
public $arr = ['a' => 'apple', 'b' => 'banana'];
public function normalFunction($key) {
return $this->arr[$key];
}
public function &referenceReturningFunction($key) {
return $this->arr[$key];
}
}
$bar = new foo();
$var = $bar->normalFunction('beer'); //Notice Error. Undefined index beer
$var = &$bar->referenceReturningFunction('beer'); // No error. The value of $bar is
now null
var_dump($bar->arr);
/**
[
"a" => "apple",
"b" => "banana",
"beer" => null,
],
*/
?>
This is in no way a "bug" - the framework is performing as designed, but it took careful
thought to figure out what was going on. PHP7.3
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.54 (proxied: 23.125.164.160)
----
Manual Page -- http://php.net/manual/en/language.references.pass.php
Edit -- https://master.php.net/note/edit/124383
Del: integrated -- https://master.php.net/note/delete/124383/integrated
Del: useless -- https://master.php.net/note/delete/124383/useless
Del: bad code -- https://master.php.net/note/delete/124383/bad+code
Del: spam -- https://master.php.net/note/delete/124383/spam
Del: non-english -- https://master.php.net/note/delete/124383/non-english
Del: in docs -- https://master.php.net/note/delete/124383/in+docs
Del: other reasons-- https://master.php.net/note/delete/124383
Reject -- https://master.php.net/note/reject/124383
Search -- https://master.php.net/manage/user-notes.php