I’m working on a wordpress plugin, that relies heavily on use of the global keyword. I’ve been asked to centralise them, without rewriting the plugin. So, I’ve written a function, similar to the below.

function globalise(){
  global $foo;
  return $foo;
}

function process_one(){
  //global $foo;
  $foo = globalise();
  $foo->counting = 123;
}

My question is:

In a separate function, called after process_one()

function process_two{
  //global $foo;
  $foo = globalise();
}

Will $foo->counting; still be equal to ‘123’, if defined using $foo = globalise();?

Read more here: Global keyword and scope in php – are these equivalent?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: