Π€ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ…

PHP ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ ΠΊΠΎΠ½Ρ†Π΅ΠΏΡ†ΠΈΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ…. Π­Ρ‚ΠΎ ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ Ссли ΠΊ ΠΈΠΌΠ΅Π½ΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ присоСдинили ΠΊΡ€ΡƒΠ³Π»Ρ‹Π΅ скобки, PHP ΠΈΡ‰Π΅Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ с Ρ‚Π΅ΠΌ ΠΆΠ΅ ΠΈΠΌΠ΅Π½Π΅ΠΌ, Ρ‡Ρ‚ΠΎ ΠΈ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ вычислСния ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ, ΠΈ ΠΏΡ€ΠΎΠ±ΡƒΠ΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ. Π­Ρ‚ΠΈΠΌ синтаксисом ΠΎΠΏΠΈΡΡ‹Π²Π°ΡŽΡ‚ callback-Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ, Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ ΠΈ Ρ‚. Π΄.

Π€ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… Π½Π΅ Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚ с языковыми конструкциями echo, print, unset(), isset(), empty(), include, require ΠΈ Ρ‚. ΠΏ. ΠΠ°ΠΏΠΈΡˆΠΈΡ‚Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ-ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΡƒ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ эти конструкции Ρ€Π°Π±ΠΎΡ‚Π°Π»ΠΈ ΠΊΠ°ΠΊ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ….

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ

<?php

function foo() {
echo
"Π’ foo()\n";
}

function
bar($arg = '')
{
echo
"Π’ bar(); Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ Π±Ρ‹Π» '$arg'.\n";
}

// Ѐункция-ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠ° для echo
function echoit($string)
{
echo
$string;
}

$func = 'foo';
$func(); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ foo()

$func = 'bar';
$func('test'); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ bar()

$func = 'echoit';
$func('test'); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ echoit()

Бинтаксис Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Ρ‚Π°ΠΊΠΆΠ΅ для Π²Ρ‹Π·ΠΎΠ²Π° ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ².

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ

<?php

class Foo
{
function
Variable()
{
$name = 'Bar';
$this->$name(); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΌΠ΅Ρ‚ΠΎΠ΄ Bar()
}

function
Bar()
{
echo
"Π­Ρ‚ΠΎ Bar";
}
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ $foo->Variable()

ΠŸΡ€ΠΈ Π²Ρ‹Π·ΠΎΠ²Π΅ статичСских ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ² Π²Ρ‹Π·ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ «сильнСС», Ρ‡Π΅ΠΌ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ доступа ΠΊ статичСскому свойству:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Π²Ρ‹Π·ΠΎΠ²Π° ΠΌΠ΅Ρ‚ΠΎΠ΄Π° ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ со статичСским свойством

<?php

class Foo
{
static
$variable = 'статичСскоС свойство';

static function
Variable()
{
echo
"Π’Ρ‹Π·ΠΎΠ² ΠΌΠ΅Ρ‚ΠΎΠ΄Π° Variable\n";
}
}

echo
Foo::$variable ."\n"; // Π­Ρ‚ΠΎ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚ «статичСскоС свойство». Π’ области видимости класса Π½ΡƒΠΆΠ½Π° пСрСмСнная $variable
$variable = "Variable";
Foo::$variable(); // Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ $foo->Variable() послС прочтСния ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $variable Π² Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ области видимости

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

статичСскоС свойство
Π’Ρ‹Π·ΠΎΠ² ΠΌΠ΅Ρ‚ΠΎΠ΄Π° Variable

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #4 Π‘Π»ΠΎΠΆΠ½Ρ‹Π΅ callable-Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ

<?php

class Foo
{
static function
bar()
{
echo
"bar\n";
}

function
baz()
{
echo
"baz\n";
}
}

$func = array("Foo", "bar");
$func(); // Π’Ρ‹Π²Π΅Π΄Π΅Ρ‚ "bar"
$func = array(new Foo(), "baz");
$func(); // Π’Ρ‹Π²Π΅Π΄Π΅Ρ‚ "baz"
$func = "Foo::bar";
$func(); // Π’Ρ‹Π²Π΅Π΄Π΅Ρ‚ "bar"
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
26
niemans at pbsolo dot nl ΒΆ
7 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
2
Anonymous ΒΆ
14 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
-2
rnealxp at yahoo dot com ΒΆ
6 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.
?>