Previously, I had a global array ($v) and referenced it from within functions by using global $v;. I now want to encapsulate everything about that array, so I wrote a class. In the global context, I instantiate the class:
$vi = new my_v();
Within a function I want to call a method of that object:
function f($x) {
$vi->add($x);
}
How do I refer to $vi within the function?
Use the global keyword:
function f($x) {
global $vi;
$vi->add($x);
}
You can also use the $GLOBALS
superglobal array:
function f($x) {
$GLOBALS['vi']->add($x);
}
See: http://us1.php.net/manual/en/language.variables.scope.php
Working example: http://3v4l.org/ERIK8