note 124750 added to language.variables.scope
| From: | ketansheladiya1@php.net | Date: | Mon, 24 Feb 2020 16:54:50 +0000 |
| Subject: | note 124750 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214213@lists.php.net to get a copy of this message | ||
As per PHP standards
The new operator returns a reference automatically, so assigning the result of new by reference is
not allowed as of PHP 7.0.0, results in an E_DEPRECATED message as of PHP 5.3.0, and an E_STRICT
message in earlier versions.
https://www.php.net/manual/en/language.operators.assignment.php
so you can use following code to check
<?php
function &get_instance_ref() {
static $obj;
echo 'Static object: ';
var_dump($obj);
if (!isset($obj)) {
// Assign a reference to the static variable
$obj = new stdclass;
}
$obj->property++;
return $obj;
}
function &get_instance_noref() {
static $obj;
echo 'Static object: ';
var_dump($obj);
if (!isset($obj)) {
// Assign the object to the static variable
$obj = new stdclass;
}
$obj->property++;
return $obj;
}
$obj1 = get_instance_ref();
$still_obj1 = get_instance_ref();
echo "\n";
$obj2 = get_instance_noref();
$still_obj2 = get_instance_noref();
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 150.129.164.240)
----
Manual Page -- http://php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/124750
Del: integrated -- https://master.php.net/note/delete/124750/integrated
Del: useless -- https://master.php.net/note/delete/124750/useless
Del: bad code -- https://master.php.net/note/delete/124750/bad+code
Del: spam -- https://master.php.net/note/delete/124750/spam
Del: non-english -- https://master.php.net/note/delete/124750/non-english
Del: in docs -- https://master.php.net/note/delete/124750/in+docs
Del: other reasons-- https://master.php.net/note/delete/124750
Reject -- https://master.php.net/note/reject/124750
Search -- https://master.php.net/manage/user-notes.php