Find the name of the caller var

Anyone have an idea, if at all possible with PHP?

function foo($var) {
    // the code here should output the value of the variable
    // and the name the variable has when calling this function
}

$hello = "World";
foo($hello);

Would give me this outlet

varName = $hello
varValue = World

EDIT

Since most people here β€œblame” me for bad practices and global variables, I’m going to talk more about why we are looking for this behavior.

the reason we look at this behavior is because we want to simplify the assignment of variables to our views.

In most cases, we do this to assign variables to our view.

$this->view->assign('products', $products);
$this->view->assign('members', $members);

Although it would be simpler and more understandable to simply be able to do the following and allow the view to be responsible for determining the name of the variable that the assigned data receives in our views.

$this->view->assign($products);
$this->view->assign($members);
+5
source share
10 answers

, , , , , . ,

$arrProducts $products?

, . , ( - ) :

$this->view->assign(strtolower($category)); 

, .

: "" , .

, :

public function __set($name, $value) {
    $this->assign($name, $value);
}

$this->view->product = $product;
+4

: .

: apd, bytekit, runkit, Reflection API debug_backtrace, , - .

- , . , , , , , , , .

, OP

+5

, - , . , . . foo , , , . $var .

: . () - , . , ( ), (, , ). , .

+2

, , . , , , , .

- , $GLOBALS.

,

function this_aint_a_good_idea_really($var) {
    print "Variable name: {$var}\n";
    print "Variable contents: {$GLOBALS[$var]}\n";
}
$hello="World";
this_aint_a_good_idea_really('hello');

, , , . ( , , , , , - )

+2

, , debug_backtrace(), tokenize script, (, - foo ( "hello $user" ). $ ($ user, 5))?),

- .

.

+1

, , , ,

<?php
class foo
{
    //Public so we can test it later
    public $bar;
    function foo()
    {
        //Init the array
        $this->bar = array();
    }
    function assign($__baz)
    {
        //Try to figure out the context
        $context = debug_backtrace();
        //assign the local array with the name and the value
        //Alternately you can initialize the variable localy
        //using $$__baz = $context[1]['object']->$__baz;
        $this->bar[$__baz] = $context[1]['object']->$__baz;
    }
}
//We need to have a calling context of a class in order for this to work
class a
{
    function a()
    {

    }
    function foobar()
    {
        $s = "testing";
        $w = new foo();
        //Reassign local variables to the class
        foreach(get_defined_vars() as $name => $val)
        {
            $this->$name = $val;
        }
        //Assign the variable
        $w->assign('s');
        //test it
        echo $w->bar['s'];
    }
}
//Testrun
$a = new a();
$a->foobar();
+1

- . , , - ,

debug_backtrace();
0

, , , :

<?php
  function assign($val)
  {
    global $$val;
    echo $$val;
  }
  $hello = "Some value";
  assign('hello');

Ouputs: Some value

0

, PHP . . , . , PHP, .

NamedVariable - , . $products = new NamedVariable('products', $productData);, $this->view->assign($products);. , , - , NamedVariable , .

, . , script, assign() . , , PHP . IDE assign(). , PHP .

0

GLOBALS. , , , .

function get_var_name(&$var, $scope=FALSE) {
    if($scope) $vals = $scope;
    else      $vals = $GLOBALS;
    $old = $var;
    $var = $new = 'unique'.rand().'value';
    $vname = FALSE;
    foreach ($vals as $key => $val) {
        if($val === $new) $vname = $key;
    }
    $var = $old;
    return $vname;
}

$testvar = "name";
echo get_var_name($testvar);  // "testvar"

function testfunction() {
    $var_in_function = "variable value";
    return get_var_name($var_in_function, get_defined_vars());
}

echo testfunction();  // "var_in_function"

class testclass {
    public $testproperty;
    public function __constructor() {
        $this->testproperty = "property value";
    }
}

$testobj = new testclass();
echo get_var_name($testobj->testproperty, $testobj);  // "testproperty"
0

All Articles