A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.php.net/manual/en/functions.variable-functions.php below:

PHP: Variable functions - Manual

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"
?>
niemans at pbsolo dot nl

6 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!
rnealxp at yahoo dot com

4 years ago

<?php
interface iface_dynamic_members{
public function __call($name, $args);
public function
__set($name, $value);
public function
quietly_fail():bool;
}
trait
trait_has_dynamic_members{
public function __call($name, $args) {
if (
is_callable($this->$name)) {
return
call_user_func($this->$name, $args);
}
else {
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; }
}
abstract class
MBR_ATTR{
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); }
}
class
cls_partly_dynamic implements iface_dynamic_members{
use
trait_has_dynamic_members;
public function
quietly_fail():bool{
return
true;
}
}
error_reporting(E_ALL & ~E_NOTICE); $obj_runner = new cls_partly_dynamic();
MBR_ATTR::is_a_runner($obj_runner, 'fast');
$obj_runner->runner_type = 'a bit slow';
$obj_walker = new cls_partly_dynamic();
MBR_ATTR::is_a_walker($obj_walker, 'slow');
$obj_walker->walker_type = 'super fast';
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>'; ?>
Anonymous

13 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()
?>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3