1

Newbie question...

Is it possible to use a function's argument to pass code to be executed inside the function? Something like...

function myfunction($somecode)
{
    $somecode;
}

myfunction("echo 'foo'");

Actual scenario: my function performs some image manipulation using Imagick...

function myfunction($imagePath)
{
    $image = new Imagick($imagePath);
    $image->gaussianBlurImage(0, 5);
}
myfunction("test.jpg");

I'd like to be able to specify the Imagick method to use on the image in a function argument, so I could have something like...

function myfunction($imagePath, $method)
{
    $image = new Imagick($imagePath);
    $image->$method;
}
myfunction("test.jpg", "thumbnailImage(100, 100)");

Is such a thing possible?

6
  • Either use eval or pass a closure which accepts $image as argument. Commented Sep 17, 2015 at 3:38
  • @mario what about Variable functions php.net/manual/en/functions.variable-functions.php Commented Sep 17, 2015 at 3:43
  • 2
    @Fred-ii- Sure, would also fit. If it's just a single extra function call, then splitting args into e.g. myfunc($img, "thumbnail", 100, 100) would even look tidier. Commented Sep 17, 2015 at 3:48
  • 2
    Variable functions are good here. Also, perhaps consider being more strict about the $methods that you will accept, and run through a switch(). That way, you can handle exceptions / unknown methods. Commented Sep 17, 2015 at 3:54
  • The method proposed by @CharlieS I feel this is probably the best way, you can insure that you have the proper number/type of arguments, provide defaults, and failing everything else issue meaningful exceptions. Commented Sep 17, 2015 at 4:06

2 Answers 2

1

Instead of using anonymous functions and variable variables you could also do this with call_user_func_array

function myfunction($imagePath, $method, array $arguments)
{
    $image = new Imagick($imagePath);
    call_user_func_array([$image, $method], $arguments);
}
myfunction("test.jpg", "thumbnailImage", [100,100]);

For additional safety you can check the methods against a whitelist of available methods for the Imagick object.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Orangepill, got this to work with a couple of changes to syntax: function myfunction($imagePath, $method, array $arguments) { $image = new Imagick($imagePath); call_user_func_array(array($image, $method), $arguments); } myfunction("test.jpg", "thumbnailImage", array(100, 100));
If the changes are changing the short array syntax, [] to array() then you are using an unsupported version of php and should update as soon as possible.
Sorry, the short array syntax is fine. It's just my code editor that's out of date - it was highlighting the [,] syntax as an error.
0

Programming PHP (Lerdorf, Tatroe, & MacIntyre), 2nd Edition. Ch. 3, p. 73

Anonymous Functions

"You can create an anonymous function using create_function()."

PHP Manual: create_function()

$lamda = create_function( '$param1,param2', 'code;' );

But, there is more to know in PHP 5.3 and later. From example #2 of the PHP Manual under Anonymous Functions.

$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

PHP Manual: Anonymous Functions

8 Comments

If you are using PHP 5.3.0 or newer a native anonymous function should be used instead.
yeah, that one I did think about mentioning to the OP ^
@Marty I assumed one would discover this by clicking on the link.
Of course, however that information seems important enough that if the link broke for whatever reason or someone passing by was too lazy to click it, a comment still follows to mention it.
@Marty Good thing Google has a going concern.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.