Folks,
Now you folks know why I hate the manual and avoid it like the Corona Plague, if Covid is a plage that is as I hearing it isn’t.
Just look at what the manual says …
**”References with global and static variables ¶
PHP implements the static and global modifier for variables in terms of references. For example, a true global variable imported inside a function scope with the global statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses:** “
It gives a very complicated code as an example for a beginner/intermediate level to understand:
[code]
<?php
function test_global_ref() {
global $obj;
$new = new stdclass;
$obj = &$new;
}
function test_global_noref() {
global $obj;
$new = new stdclass;
$obj = $new;
}
test_global_ref();
var_dump($obj);
test_global_noref();
var_dump($obj);
?>
Now, who can show me a beginner level procedural style code for me to understand ?
Here is another rocket science the manual expects me to understand ….
[code]
<?php
function &get_instance_ref() {
static $obj;
echo ‘Static object: ‘;
var_dump($obj);
if (!isset($obj)) {
$new = new stdclass;
// Assign a reference to the static variable
$obj = &$new;
}
if (!isset($obj->property)) {
$obj->property = 1;
} else {
$obj->property++;
}
return $obj;
}
function &get_instance_noref() {
static $obj;
echo ‘Static object: ‘;
var_dump($obj);
if (!isset($obj)) {
$new = new stdclass;
// Assign the object to the static variable
$obj = $new;
}
if (!isset($obj->property)) {
$obj->property = 1;
} else {
$obj->property++;
}
return $obj;
}
$obj1 = get_instance_ref();
$still_obj1 = get_instance_ref();
echo “n”;
$obj2 = get_instance_noref();
$still_obj2 = get_instance_noref();
?>
Never in my entire life came across something like this “function &get_instance_ref()”, although have come across something like this:
[code]
“function get_instance_ref(&$param) ”