Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create_function instead of lambda-function avartaco

Tags:

php

I was trying to implement avartaco, which is like gravatar.

In order to make it work in php version < 5.3

If you want to make it work on PHP less than 5.3.0, find string

array_walk($shape, function(&$coord, $index, $mult) { $coord *= $mult; }, self::SPRITE_SIZE);

and rewrite it for using create_function() instead of lambda-function.

I was getting error Parse error: syntax error, unexpected T_FUNCTION in that same line array_walk.My php version is 5.2.17 <5.3 But I have no idea what is meant by rewriting by createfunction?

So what should I change in that line to make it work in php version < 5.3

private function GetShape($type) {

    switch($type) {

        case 'side':

            $shape_id = hexdec(substr($this->_hash, 22, 1)) & (sizeof($this->_shapesSide) - 1);

            $shapes = $this->_shapesSide;
        break;
        case 'center':
            $shape_id = hexdec(substr($this->_hash, 23, 1)) & (sizeof($this->_shapesCenter) - 1);

            $shapes = $this->_shapesCenter;
        break;

        case 'corner':
            $shape_id = hexdec(substr($this->_hash, 24, 1)) & (sizeof($this->_shapesCorner) - 1);

            $shapes = $this->_shapesCorner;
        default:
        break;

    }

    $shape = $shapes[$shape_id];
    
    array_walk($shape, function(&$coord, $index, $mult) { $coord *= $mult; }, self::SPRITE_SIZE);
    return $shape;
    
}
like image 333
S P Avatar asked Sep 10 '13 15:09

S P


2 Answers

Closures were not introduced until PHP 5.3.

Since you are running PHP 5.2.17, you need to rewrite array_walk() to use create_function() (as the docs indicated).

array_walk(
  $shape,
  create_function('&$coord, $index, $mult', '$coord *= $mult'),
  self::SPRITE_SIZE
);

Note: I condensed the function as you were not using $index. Forgot this was a callback, so the parameters matter.

Please considering updating to at least PHP 5.3.

like image 190
Jason McCreary Avatar answered Nov 05 '22 14:11

Jason McCreary


Simply do the following

array_walk(
    $shape,
    create_function(
        '&$coord, $index, $mult',
        '$coord *= $mult;'
    ),
    self::SPRITE_SIZE
);

I have tested avatarico in php < 5.3 and it works!

like image 5
Santosh Avatar answered Nov 05 '22 13:11

Santosh