Create_function instead of lambda function avartaco

I tried to implement avartaco , which is similar to gravatar.

To make it work in php version <5.3

If you want it to work in PHP less than 5.3.0, find the line

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

and rewrite it to use create_function () instead of the lambda function.

I got Parse error: syntax error, unexpected T_FUNCTION on the same line array_walk.My php version 5.2.17 <5.3 But I have no idea what I mean when rewriting with createfunction?

So what should I change in this 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; } 
+7
php
source share
3 answers

Just follow these steps

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

I tested the avatar in php <5.3 and it works!

+5
source share

Closures were not introduced until PHP 5.3.

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

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

Note: I configured the function since you did not use $index . It was a callback, so the parameters matter.

Please consider upgrading to at least PHP 5.3.

+7
source share

alternatively you can use the array_walk callback function this way if you are below PHP 5.3

 function array_walk_callback(&$coord, $mult){ $coord *= $mult; } array_walk($shape, 'array_walk_callback', self::SPRITE_SIZE); 
+3
source share

All Articles