Funkcje zmiennych

PHP obsługuje opcję funkcji zmiennych. Oznacza to, że jeśli do nazwy zmiennej dodane są nawiasy, PHP postara się znaleźć funkcję o nazwie takiej jak wartość zmiennej i spróbuje ją wykonać. Między innymi, może to służyć do zaimplementowania tzw. callbacks, tablic funkcji, a także wielu innych rzeczy.

Funkcje zmiennych nie zadziałają z takimi elementami języka jak echo, print, unset(), isset(), empty(), include, require i podobnymi. Używaj okrężnych funkcji aby skorzystać z któregoś z powyższych elementów języka jako funkcji zmiennych.

Przykład #1 Przykład funkcji zmiennych

<?php
function foo() {
    echo 
"W foo()<br />\n";
}

function 
bar($arg '')
{
    echo 
"W bar(); argumentem było '$arg'.<br />\n";
}

// To jest funkcja okrężna dla "echo"
function echoit($string)
{
    echo 
$string;
}

$func 'foo';
$func();        // Wywołuje foo()

$func 'bar';
$func('test');  // Wywołuje bar()

$func 'echoit';
$func('test');  // Wywołuje echoit()
?>

Jako funkcje zmiennych mogą być także wywoływane metody obiektów.

Przykład #2 Przykład metod zmiennych

<?php
class Foo
{
    function 
Zmienna()
    {
        
$name 'Bar';
        
$this->$name(); // Wywołuje metodę Bar()
    
}
    
    function 
Bar()
    {
        echo 
"To jest Bar";
    }
}

$foo = new Foo();
$funcname "Zmienna";
$foo->$funcname();  // Wywołuje $foo->Zmienna()

?>

Gdy wywołuje się metody statyczne, wywołanie funkcji jest mocniejsze niż operator własności statycznej:

Przykład #3 Przykład zmiennej metody z właściwościami statycznymi

<?php
class Foo
{
    static 
$zmienna 'własność statyczna';
    static function 
Zmienna()
    {
        echo 
'Wywołano metodę Zmienna';
    }
}

echo 
Foo::$zmienna// Wyświetli 'własność statyczna'. Potrzebna jest $zmienna w tym zasięgu.
$zmienna "Zmienna";
Foo::$variable();  // Wywoła $foo->Zmienna() odczytując $zmienna w tym zasięgu.

?>

Od PHP 5.4.0 możesz wywołać dowolną wartość typu callable trzymaną w zmiennej.

Przykład #4 Kompleksowy przykład callables

<?php
class Foo
{
    static function 
bar()
    {
        echo 
"bar\n";
    }
    function 
baz()
    {
        echo 
"baz\n";
    }
}

$func = array("Foo""bar");
$func(); // wyświetli "bar"
$func = array(new Foo"baz");
$func(); // wyświetli "baz"
$func "Foo::bar";
$func(); // wyświetli "bar" od PHP 7.0.0; wcześniej wygeneruje fatal error
?>

Zobacz także is_callable(), call_user_func(), zmienne zmienne oraz function_exists().

Rejestr zmian

Wersja Opis
7.0.0 'NazwaKlasy::nazwaMetody' jest dozwoloną fukcją zmienną.
5.4.0 Tablice, które są poprawnymi callables, są dozwolone jako zmienne funkcje.

add a note add a note

User Contributed Notes 6 notes

up
5
niemans at pbsolo dot nl
5 years ago
While the documentation suggests that the use of a constant is similar to the use of a variable, there is an exception regarding variable functions. You cannot use a constant as the function name to call a variable function.

const DEBUGME ='func';
function func($s) { echo $s. "\n"; }

DEBUGME('abc');  // results in a syntax error

$call = DEBUGME;
$call('abc');          // does the job

But you can use a constant as an argument to a function. Here's a simple workaround when you need to call a variable constant function:

function dynamic($what, $with)
   {
     $what($with);
   }
dynamic(DEBUGME, 'abc');

This makes sense to me to hide API's and/or long (complicated) static calls.
Enjoy!
up
6
Anonymous
12 years ago
$ wget http://www.php.net/get/php_manual_en.tar.gz/from/a/mirror
$ grep -l "\$\.\.\." php-chunked-xhtml/function.*.html

List of functions that accept variable arguments.
<?php
array_diff_assoc
()
array_diff_key()
array_diff_uassoc()
array()
array_intersect_ukey()
array_map()
array_merge()
array_merge_recursive()
array_multisort()
array_push()
array_replace()
array_replace_recursive()
array_unshift()
call_user_func()
call_user_method()
compact()
dba_open()
dba_popen()
echo()
forward_static_call()
fprintf()
fscanf()
httprequestpool_construct()
ibase_execute()
ibase_set_event_handler()
ibase_wait_event()
isset()
list()
maxdb_stmt_bind_param()
maxdb_stmt_bind_result()
mb_convert_variables()
newt_checkbox_tree_add_item()
newt_grid_h_close_stacked()
newt_grid_h_stacked()
newt_grid_v_close_stacked()
newt_grid_v_stacked()
newt_win_choice()
newt_win_entries()
newt_win_menu()
newt_win_message()
newt_win_ternary()
pack()
printf()
register_shutdown_function()
register_tick_function()
session_register()
setlocale()
sprintf()
sscanf()
unset()
var_dump()
w32api_deftype()
w32api_init_dtype()
w32api_invoke_function()
wddx_add_vars()
wddx_serialize_vars()
?>
up
-3
rnealxp at yahoo dot com
3 years ago
<?php
/*
You might have found yourself at this php variable functions page because, like me, you wanted to pass functions
around like objects to client objects as you can in JavaScript. The issue I ran into was although
I could call a function using a variable like this " $v(); "...I could not do it like this " $obj->p() " where
'p' is a property containing the name of the method to call. Did not want to save my property off to a variable prior
to making my call: " $v = $obj->p; $v(); "; even if one finds a way, the below applies...

I credit this expanded work to this person: tatarynowicz at gmail dot com;
without them I would not have gotten here.
*/
interface iface_dynamic_members{
   
//Use of this interface enables type-hinting for objects that implement it.
   
public function __call($name, $args);
    public function
__set($name, $value);
    public function
quietly_fail():bool;
}
trait
trait_has_dynamic_members{
   
//Implementing these magic methods in the form of a trait, frees the client object up
    //so it can still inherit from a parent-class.
   
public function __call($name, $args) {
        if (
is_callable($this->$name)) {
            return
call_user_func($this->$name, $args);
        }
        else {
           
//Your dynamic-membered object can declare itself as willing to ignore non-existent method calls or not.
           
if($this->quietly_fail()===true){
                echo
'Method does not exist, but I do not mind.';
            }else{
                echo
'Method does not exist, I consider this a bug.';
            }
        }
    }
    public function
__set($name, $value) {
       
$this->$name = is_callable($value) ? $value->bindTo($this, $this): $value; //Assignment using ternary operator.
   
}
}
abstract class
MBR_ATTR{
   
//A class full of attributes that objects can take on; abstract since not to be instantiated (If I could make it "final" as well, I would).
   
public static function is_a_walker(iface_dynamic_members $obj, ?string $walker_type='normal pace'){
       
$obj->walker_type = $walker_type;
       
$obj->walker_walk = function() {
            return
"I am walking {$this->walker_type}.";
        };
    }
    public static function
is_a_runner(iface_dynamic_members $obj, string $runner_type){
       
$obj->runner_type = $runner_type;
       
$obj->runner_run = function() {
            return
"I am running {$this->runner_type}.";
        };
       
self::is_a_walker($obj); //If can run, also can walk.
   
}
}
class
cls_partly_dynamic implements iface_dynamic_members{
    use
trait_has_dynamic_members;
    public function
quietly_fail():bool{
        return
true;
    }
}
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE); //Enable all error-reporting except notices.
//----
//config runner object...
$obj_runner = new cls_partly_dynamic();
MBR_ATTR::is_a_runner($obj_runner, 'fast');
$obj_runner->runner_type = 'a bit slow';
//----
//config walker object...
$obj_walker = new cls_partly_dynamic();
MBR_ATTR::is_a_walker($obj_walker, 'slow');
$obj_walker->walker_type = 'super fast';
//----
//Do stuff...
echo 'walker in action...' . '<br>';
echo
$obj_walker->walker_walk() . '<br>';
echo
'<br>';
echo
'runner in action...' . '<br>';
echo
$obj_runner->walker_walk() . '<br>';
echo
$obj_runner->runner_run() . '<br>';
echo
$obj_runner->xxx() . '<br>'; //Try calling a non-existent method.
//I would agree that the above approach/technique is not always ideal, particulary due to the loss of code-completion in your
//IDE of choice; I would tend to use this approach for dynamic-programming in response to the user dictating processing steps via a UI.
?>
up
-4
Anonymous
3 years ago
If you are here looking for a function reference, this is NOT how to do it:

<?php
function func1(){ echo "hell0 1";}
$choice = func1; // no quotes
?>

It works, but $choice is not what you might think, a reference to a function. It is simply the name of the function as a string, written without (!) quotes.

It's  the same as
<?php
$choice
= "func1"; // with quotes
?>

You can do echo gettype($choice) to confirm.

So calling
<?php
$choice
()
?>
is a variable-function for both cases, calling it by its name, not by reference.

Go via an assigned anonymous function to get a reference to the function:
<?php
$func1
= function(){ echo "hell0 1";}
$func1 = function(){ echo "hell0 2";}
?>

Now you can pass around the function like a first class object
<?php
$choice
= $func1;
?>
or
<?php
$choice
= $func2;
?>
and call it
<?php
$choice
();
?>

If you want to pass around a class method, use the "Complex callables" from the manual, above. It's a call by name (not a reference), but since you can include the object you can still get the flexibility you want:

<?php
class C {
      function
k(){ echo "inside k";}
      function
j(){ echo "inside j"; return  array($this,"k");}};
?>

You can use $this as the object in the first element of the array.
<?php
$c
= new C;
$c->k();
inside k

$func
= $c->j();
inside j
?>
And now, le moment supreme:
<?php
$func
();
inside k
?>
up
-17
josh at joshstroup dot xyz
7 years ago
A small, but helpful note. If you are trying to call a static function from a different namespace, you must use the fully qualified namespace, even if they have the same top level namespace(s). For example if you have the following class to call:

<?php
namespace Project\TestClass;
class
Test {
    static function
funcToCall() {
        return
"test";
    }
}
?>
You must call it as:
<?php
namespace Project\OtherTestClass;
class
OtherTest {
    static function
callOtherFunc() {
       
$func = '\Project\TestClass::funcToCall';
       
$func();
    }
}
?>
and not:
<?php
class OtherTest {
    static function
callOtherFunc() {
       
$func = 'TestClass::funcToCall';
       
$func();
    }
}
?>
up
-26
boards at gmail dot com
18 years ago
If you want to call a static function (PHP5) in a variable method:

Make an array of two entries where the 0th entry is the name of the class to be invoked ('self' and 'parent' work as well) and the 1st entry is the name of the function.  Basically, a 'callback' variable is either a string (the name of the function) or an array (0 => 'className', 1 => 'functionName').

Then, to call that function, you can use either call_user_func() or call_user_func_array().  Examples:

<?php
class A {

  protected
$a;
  protected
$c;

  function
__construct() {
   
$this->a = array('self', 'a');
   
$this->c = array('self', 'c');
  }

  static function
a($name, &$value) {
    echo
$name,' => ',$value++,"\n";
  }

  function
b($name, &$value) {
   
call_user_func_array($this->a, array($name, &$value));
  }

  static function
c($str) {
    echo
$str,"\n";
  }

  function
d() {
   
call_user_func_array($this->c, func_get_args());
  }

  function
e() {
   
call_user_func($this->c, func_get_arg(0));
  }

}

class
B extends A {

  function
__construct() {
   
$this->a = array('parent', 'a');
   
$this->c = array('self', 'c');
  }

  static function
c() {
   
print_r(func_get_args());
  }

  function
d() {
   
call_user_func_array($this->c, func_get_args());
  }

  function
e() {
   
call_user_func($this->c, func_get_args());
  }

}

$a =& new A;
$b =& new B;
$i = 0;

A::a('index', $i);
$a->b('index', $i);

$a->c('string');
$a->d('string');
$a->e('string');

# etc.
?>
To Top