I do a lot of stuff specifically with PHP and WordPress, and I've come across this concept quite often throughout the WordPress application; although, this question isn't specific to WordPress. I'll demonstrate with an example.
Say there is some global object of a class.
global $foo;
$foo = new Foo();
And then this class has some sort of accessor method that could be used with our global object.
global $foo;
$bar = $foo->get_bar();
Well, often I see this wrapped in a general function that takes care of tapping into the object's method, essentially hiding that part of it, making it more simple to access that item.
function wp_get_bar() {
global $foo;
return $foo->get_bar();
}
And so a developer using the application could just do this where ever they wanted without knowing about the $foo global object specifically:
$bar = wp_get_bar();
I'm just curious if this is a general computer science concept? If so, is there a name for it? Or is this something I can read more about somewhere?
When Wordpress was created, OOP wasn't very popular. In fact, OOP became more serious with PHP5, and Wordpress started with... PHP3?
In addition, I think that to make it easy for newbie users, Wordpress developers opted using procedural functions instead of objects and classes. It's simpler if you don't have a deep knowledge of WP core.
I don't think this has an specific name.
To answer your direct question; no there is no official term for accessing variables via global construct within functions, unofficial? How about "Way not to do it".
With that being said, the same thing can be accomplished, properly, using a singleton pattern:
class Foo {
protected $bar;
public function setBar($bar)
{
$this->bar = $bar;
}
public function getBar()
{
return $this->bar;
}
}
class Foo_Package
{
public static $foo;
}
function functionOne($params)
{
var_dump(Foo_Package::$foo->getBar());
}
function functionTwo($id)
{
var_dump(Foo_Package::$foo->getBar());
}
Foo_Package::$foo = new Foo();
$foo->setBar('Hi I\'m Bar!');
Foo_Package::$foo = $foo;
functionOne(array('id' => 1));
functionTwo(1);