Probably a dumb way to do it, but you could use an overloaded class with a static variable (so kind of like a global...). I only suggest overloading because it works like a regular variable (in how you assign it).
I'm sure someone who has worked with PHP longer than I have will have a better idea. Also I am not sure what "older version" means (how old "old" is) but the overloading goes back to at least 5.0/5.1. I think the closure is around 5.3 (according to this answer Which version of php add anonymous functions)? This is not exactly the scenario, you have outlined, but the accessing of the value is made available without using a true global
:
class Overlord
{
private static $properties;
public static $val;
public function __get($property)
{
return (isset(self::$properties[$property]))? self::$properties[$property]:false;
}
public function __set($property,$value)
{
self::$val = self::$properties[$property] = $value;
}
public function __isset($property)
{
return (isset(self::$properties[$property]));
}
}
function myFunction($e) {
return Overlord::$val." overloaded PLUS -->".$e;
}
// Create the mighty overloader class
$ov = new Overlord();
// Assign a variable
$ov->val = 21;
// Random well-thought-out array
$myArray = array("One",1,"Two",2);
// Run through the first variable
$x = array_map("myFunction", $myArray);
// Assign a second random variable
$ov->stuff = 11;
// Run through the array again with new variable
$y = array_map("myFunction", $myArray);
// Array one
print_r($x);
// Array two
print_r($y);
Would give you something like:
Array
(
[0] => 21 overloaded PLUS -->One
[1] => 21 overloaded PLUS -->1
[2] => 21 overloaded PLUS -->Two
[3] => 21 overloaded PLUS -->2
)
Array
(
[0] => 11 overloaded PLUS -->One
[1] => 11 overloaded PLUS -->1
[2] => 11 overloaded PLUS -->Two
[3] => 11 overloaded PLUS -->2
)