Variable functions

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like. Utilize wrapper functions to make use of any of these constructs as variable functions.

Example #1 Variable function example

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

function 
bar($arg '')
{
    echo 
"In bar(); argument was '$arg'.<br />\n";
}

// This is a wrapper function around echo
function echoit($string)
{
    echo 
$string;
}

$func 'foo';
$func();        // This calls foo()

$func 'bar';
$func('test');  // This calls bar()

$func 'echoit';
$func('test');  // This calls echoit()
?>

Object methods can also be called with the variable functions syntax.

Example #2 Variable method example

<?php
class Foo
{
    function 
Variable()
    {
        
$name 'Bar';
        
$this->$name(); // This calls the Bar() method
    
}
    
    function 
Bar()
    {
        echo 
"This is Bar";
    }
}

$foo = new Foo();
$funcname "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?>

When calling static methods, the function call is stronger than the static property operator:

Example #3 Variable method example with static properties

<?php
class Foo
{
    static 
$variable 'static property';
    static function 
Variable()
    {
        echo 
'Method Variable called';
    }
}

echo 
Foo::$variable// This prints 'static property'. It does need a $variable in this scope.
$variable "Variable";
Foo::$variable();  // This calls $foo->Variable() reading $variable in this scope.

?>

Example #4 Complex callables

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

$func = array("Foo""bar");
$func(); // prints "bar"
$func = array(new Foo"baz");
$func(); // prints "baz"
$func "Foo::bar";
$func(); // prints "bar"
?>

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial