/    Sign up×
Community /Pin to ProfileBookmark

Static & Global Modifier Referencing

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);
?>

[/code]

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();
?>

[/code]

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) ”
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 22.2021 — @Sempervivum,

I believe you have fair experience with:

**References with global and static variables.**

Yes ?
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...