Find the name of a calling var

php

Solution

Regardless of my doubt that this is even possible, I think that forcing a programmer on how to name his variables is generally a bad idea. You will have to answer questions like

Why can't I name my variable `$arrProducts` instead of `$products` ?

You would also get into serious trouble if you want to put the return value of a function into the view. Imagine the following code in which (for whatever reason) the category needs to be lowercase:

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

This would not work with what you're planning.

My answer therefore: Stick to the 'verbose' way you're working, it is a lot easier to read and maintain.

If you can't live with that, you could still add a magic function to the view:

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

Then you can write

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

Problem

Anyone has an idea if this is 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 output ``` varName = $hello varValue = World ``` EDIT Since most people here 'accuse' me of bad practices and global variables stuff i'm going to elaborate a little further on why we are looking for this behaviour. the reason we are looking at this kind of behaviour is that we want to make assigning variables to our Views easier. Most of the time we are doing this to assign variables to our view ``` $this->view->assign('products', $products); $this->view->assign('members', $members); ``` While it would be easier and more readable to just be able to do the following and let the view be responsible to determining the variable name the assigned data gets in our views. ``` $this->view->assign($products); $this->view->assign($members); ```

Original source