3

Anonymous function can be used as callback for function eg. array_map and one of its advantage is usage of use to use a variable from outside of the function. Eg.:

$var = 10;
$x = array_map(function($e) use ($var) { if($var > 5) return $e['prop']; }, $myArray);

What about in older PHP version when callback has to be an existing function and function eg. array_map receives the function name as callback argument?

$var = 10;
$x = array_map("myFunction", $myArray);

function myFunction($e) {
    //how to get $var here?
}

Please keep in mind that I'm looking for solution other than using global variable. Thanks.

maspai
  • 397
  • 6
  • 17
  • but what are you actual trying to achieve? –  Nov 21 '15 at 02:41
  • I would use `$GLOBALS['var']`. `$GLOBALS` is a superglobal variable (like `$_GET` or `$_POST`), therefore available from every function. Each variable is an index in this superglobal. – Iansus Nov 23 '15 at 10:39
  • @Dagon: I'd like to achieve the same like using anonymous function with `use`, but in older PHP which doesn't support anonymous function. – maspai Nov 25 '15 at 02:59

1 Answers1

1

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
)
Community
  • 1
  • 1
Rasclatt
  • 12,498
  • 3
  • 25
  • 33