Neon Physics
Neon Physics

Reputation: 3477

Dynamic function calls in PHP

Is it possible to have dynamic function calls in PHP? I don't know if I am calling it the right name, but my example hopefully will explain what I want.

<?
    function image_filename(){
        global $the_image;
        return return $the_image->filename;
    }
    function image_anchor(){
        global $the_image;
        return getAnchor($the_image->id);
    }
    // is there a way to make a function that will do something like this:
    // I know it's possible using a class and __call, but is it possible for a general case
    function image_REQUEST(){
        global $the_image;
        $args = func_get_args();
        switch(REQUEST){
            case "filename":
                return $the_image->filename;
            break;
            case "anchor":
                return getAnchor($the_image->id);
            break;
        }
    }
?>

Clarification:

I know about variable functions, and call_user_func. These are not what I am looking for. Basically, I don't want to define image_filename or image_anchor, but have them defined when they are called.

Upvotes: 1

Views: 2703

Answers (8)

aib
aib

Reputation: 46921

I don't understand why you don't just make REQUEST a parameter but you can define functions inside an eval() call.

function makeFunction($name)
{
    $functionName = "process_{$name}";

    eval("
        function {$functionName}() {
            echo \"Hi, this is {$functionName}.\\n\";
        }
    ");
}

makeFunction("Hello");

process_Hello();

Good luck with the escapes.

Upvotes: 0

mfonda
mfonda

Reputation: 7993

There is no way to define "magic" or "dynamic" functions (like with __call) for functions not defined within a class. You can however call functions dynamically. There are several ways to do this- I would recommend call_user_func_array() function, which lets you call a function, passing its arguments as an array.

For example:

$type = 'filename';
call_user_func_array("image_$type", $args);

For more info, see http://php.net/manual/en/function.call-user-func-array.php

Upvotes: 3

Ming-Tang
Ming-Tang

Reputation: 17651

You mean variable functions?

<?php

function user_func($x) { echo $x; }

$x = "user_func";
$x(1);

?>

http://php.net/manual/en/functions.variable-functions.php

To dynamically create functions, use create_function (http://ca3.php.net/create-function):

<?php
$func = create_function('$x', 'echo $x;');
$func(1);

?>

You can store them in arrays:

<?php
$funcs = array();
$funcs['error'] = create_function('$x', 'echo $x;');
?>

Upvotes: 1

Stoic
Stoic

Reputation: 10754

Use something like:

$image_request = 'image_' + REQUEST;
echo $image_request();
// lets say REQUEST = filename, then above will echo the result of function: image_filename();

These are known as variable functions, and the basic is that you store the function's name in a variable, lets say $var, and then call the function using: $var().

Also, as you state in your comment to BoltClock, if you are interested in a kind of a dynamic function set, why not use something like this:

function image_functions(REQUEST) {
    switch (REQUEST) {
    // ...
    }
}

Upvotes: 0

Matthew Purdon
Matthew Purdon

Reputation: 773

You can also do simple string replacements.

<?php
$action = $_REQUEST['action'];
$functionName = 'image_' . $action;
if (function_exists($functionName)) {
    $functionName($the_image);
} else {
  echo "That function is not available";
}

And that's about it. I added the extra error checking so that you don't try to run functions that don't exist.

Upvotes: 0

Andrew Sledge
Andrew Sledge

Reputation: 10351

http://php.net/manual/en/function.call-user-func.php

<?php
function increment(&$var)
{
    $var++;
}

function decrement(&$var)
{
    $var--;
}

$a = 5;
$func = "increment";
call_user_func($func, $a);
echo $a."\n";

$func = "decrement";
call_user_func($func, $a);
call_user_func($func, $a);
echo $a."\n";;
?>

produces

6
4

Upvotes: 0

Jonas Schmid
Jonas Schmid

Reputation: 5491

Is this what you are looking for ?

function foo() {
// code ...
}

$functionName = "foo";

$functionName();

Upvotes: 0

DampeS8N
DampeS8N

Reputation: 3621

Yes.

But don't. unless you like having future programmers hunt you down and gut you on the sidewalk.

Upvotes: 0

Related Questions