Hướng dẫn name variable php

Overview

In PHP, you can just put an extra $ in front of a variable to make it a dynamic variable :

Nội dung chính

  • Differences between PHP5 and PHP7
  • Case 1 : $$foo['bar']['baz']
  • Case 2 : $foo->$bar['baz']
  • Case 3 : $foo->$bar['baz']()
  • Case 4 : Foo::$bar['baz']()
  • 5.5.3. Discussion

$$variableName = $value;

While I wouldn't recommend it, you could even chain this behavior :

$$$$$$$$DoNotTryThisAtHomeKids = $value;

You can but are not forced to put $variableName between {} :

${$variableName} = $value;

Using {} is only mandatory when the name of your variable is itself a composition of multiple values, like this :

${$variableNamePart1 . $variableNamePart2} = $value;

It is nevertheless recommended to always use {}, because it's more readable.

Differences between PHP5 and PHP7

Another reason to always use {}, is that PHP5 and PHP7 have a slightly different way of dealing with dynamic variables, which results in a different outcome in some cases.

In PHP7, dynamic variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the mix of special cases in PHP5. The examples below show how the order of evaluation has changed.

Case 1 : $$foo['bar']['baz']

  • PHP5 interpetation : ${$foo['bar']['baz']}
  • PHP7 interpetation : ${$foo}['bar']['baz']

Case 2 : $foo->$bar['baz']

  • PHP5 interpetation : $foo->{$bar['baz']}
  • PHP7 interpetation : $foo->{$bar}['baz']

Case 3 : $foo->$bar['baz']()

  • PHP5 interpetation : $foo->{$bar['baz']}()
  • PHP7 interpetation : $foo->{$bar}['baz']()

Case 4 : Foo::$bar['baz']()

  • PHP5 interpetation : Foo::{$bar['baz']}()
  • PHP7 interpetation : Foo::{$bar}['baz']()

5.5.3. Discussion

The previous example prints 103. Because $animal = 'turtles', $$animal is $turtles, which equals 103.

Using curly braces, you can construct more complicated expressions that indicate variable names:

$stooges = array('Moe','Larry','Curly');
$stooge_moe = 'Moses Horwitz';
$stooge_larry = 'Louis Feinberg';
$stooge_curly = 'Jerome Horwitz';

foreach ($stooges as $s) {
  print "$s's real name was ${'stooge_'.strtolower($s)}.\n";
}
Moe's real name was Moses Horwitz.
Larry's real name was Louis Feinberg.
Curly's real name was Jerome Horwitz.

PHP evaluates the expression between the curly braces and uses it as a variable name. That expression can even have function calls in it, such as strtolower( ).

Variable variables are also useful when iterating through similarly named variables. Say you are querying a database table that has fields named title_1, title_2, etc. If you want to check if a title matches any of those values, the easiest way is to loop through them like this:

for ($i = 1; $i <= $n; $i++) {
    $t = "title_$i";
    if ($title == $$t) { /* match */ }
}

Of course, it would be more straightforward to store these values in an array, but if you are maintaining old code that uses this technique (and you can't change it), variable variables are helpful.

The curly brace syntax is also necessary in resolving ambiguity about array elements. The variable variable $$donkeys[12] could have two meanings. The first is "take what's in the 12th element of the $donkeys array and use that as a variable name." Write this as: ${$donkeys[12]}. The second is, "use what's in the scalar $donkeys as an array name and look in the 12th element of that array." Write this as: ${$donkeys}[12].

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

function foo() {
    echo 
"In foo()\n";
}

function

bar($arg '')
{
    echo 
"In bar(); argument was '$arg'.\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

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

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

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

3 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!

Anonymous

11 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.
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()
?>

rnealxp at yahoo dot com

2 years ago

/*
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...' . '
'
;
echo
$obj_walker->walker_walk() . '
'
;
echo
'
'
;
echo
'runner in action...' . '
'
;
echo
$obj_runner->walker_walk() . '
'
;
echo
$obj_runner->runner_run() . '
'
;
echo
$obj_runner->xxx() . '
'
; //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.
?>

anisgazig at gmail dot com

6 months ago

static bar method works with statically or non statically wherese non static method could not work statically//non static method
class myCls{
    public function 
foo(){
        echo
"foo function";
    }
    static public function
bar(){
        echo
"static bar function";

            }
}

$a = new myCls;
$myf = "foo";
$a->$myf();//works
echo "\n";$a = [new myCls,"foo"];//works
$a();
echo
"\n";
//$a = ["myCls","foo"];//fatal error
//$a();
echo "\n";
//$a = "myCls::foo";//fatal error
//$a();
echo "\n";//static method called with statically or nonstatically
$b = new myCls;
$myf = "bar";
$b->$myf();//works
echo "\n";$b = [new myCls,"bar"];//works
$b();
echo
"\n";$b = "myCls::bar";//works
$b();
echo
"\n";$b = ["myCls","bar"];//works
$b();
echo
"\n";

Anonymous

2 years ago

If you are here looking for a function reference, this is NOT how to do it:

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
$choice = "func1"; // with quotes
?>

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

So calling
$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:
$func1 = function(){ echo "hell0 1";}
$func1 = function(){ echo "hell0 2";}
?>

Now you can pass around the function like a first class object
$choice = $func1;
?>
or
$choice = $func2;
?>
and call it
$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:

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.
$c = new C;
$c->k();
inside k

$func

= $c->j();
inside j
?>
And now, le moment supreme:
$func();
inside k
?>

josh at joshstroup dot xyz

6 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:

namespace Project\TestClass;
class
Test {
    static function
funcToCall() {
        return
"test";
    }
}
?>
You must call it as:
namespace Project\OtherTestClass;
class
OtherTest {
    static function
callOtherFunc() {
       
$func = '\Project\TestClass::funcToCall';
       
$func();
    }
}
?>
and not:
class OtherTest {
    static function
callOtherFunc() {
       
$func = 'TestClass::funcToCall';
       
$func();
    }
}
?>