Retrieving variable name? - PHP

This is a discussion on Retrieving variable name? - PHP ; is it possible to retrieve the name of a variable passed into a function from within the function? <? function example($input) { //for example here can I determine that $input came from $a in the previous scope? } example($a); ?> ...

+ Reply to Thread
Results 1 to 7 of 7

Retrieving variable name?

  1. Default Retrieving variable name?

    is it possible to retrieve the name of a variable passed into a
    function from within the function?

    <?
    function example($input) {
    //for example here can I determine that $input came from $a in the
    previous scope?
    }

    example($a);

    ?>


    Jeffrey Sambells
    Director of Research and Development
    We-Create Inc.
    519.897.2552 cell
    519.745.7374 office
    888.615.7374 toll free
    http://www.wecreate.com

  2. Default Re: Retrieving variable name?

    "Jeffrey Sambells" <jeff@wecreate.com> wrote in message
    news:fdc497e6ac625c59bda4643eaea6aa67@wecreate.com...
    > is it possible to retrieve the name of a variable passed into a function
    > from within the function?


    Short Answer : No
    Longer Answer : Maybe, if you have knowledge of PHP internals and a
    willingness to write an extension. Even then it may not work.. <g>

    l0t3k

  3. Default Re: [PHP] Re: Retrieving variable name?

    > Short Answer : No
    > Longer Answer : Maybe, if you have knowledge of PHP internals and a
    > willingness to write an extension. Even then it may not work.. <g>



    Well, PHP5's magic methods __get()/ __set() could be used to resolve the
    variable's name...

  4. Default Re: [PHP] Retrieving variable name?

    > is it possible to retrieve the name of a variable passed into a
    > function from within the function?


    Sure. Use debug_backtrace to figure out what line and what file the
    caller is in, then read that file, find that line, find the function
    call within that line, and read what ever is between the parentheses.

    Can't think of why you'd want to do this, though...

  5. Default Re: Retrieving variable name?

    Maybe something fancy with references?

    http://us2.php.net/manual/en/language.references.php

    On 9/21/05, Thorsten Suckow-Homberg <ts@siteartwork.de> wrote:
    > > Short Answer : No
    > > Longer Answer : Maybe, if you have knowledge of PHP internals and a
    > > willingness to write an extension. Even then it may not work.. <g>

    >
    >
    > Well, PHP5's magic methods __get()/ __set() could be used to resolve the
    > variable's name...
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >


  6. Default Re: [PHP] Re: Retrieving variable name?

    oh well, thanks for the help.

    Jeffrey Sambells
    Director of Research and Development

    We-Create Inc.
    519.897.2552 cell
    519.745.7374 office
    888.615.7374 toll free
    http://www.wecreate.com

    On 21-Sep-05, at 6:02 PM, Jake Gardner wrote:

    > Maybe something fancy with references?
    >
    > http://us2.php.net/manual/en/language.references.php
    >
    > On 9/21/05, Thorsten Suckow-Homberg <ts@siteartwork.de> wrote:
    >>> Short Answer : No
    >>> Longer Answer : Maybe, if you have knowledge of PHP internals and a
    >>> willingness to write an extension. Even then it may not work.. <g>

    >>
    >>
    >> Well, PHP5's magic methods __get()/ __set() could be used to resolve
    >> the
    >> variable's name...
    >>
    >> --
    >> PHP General Mailing List (http://www.php.net/)
    >> To unsubscribe, visit: http://www.php.net/unsub.php
    >>
    >>

    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >


  7. Default Re: [PHP] Retrieving variable name?

    > Sure. Use debug_backtrace to figure out what line and what file the
    > caller is in, then read that file, find that line, find the function
    > call within that line, and read what ever is between the parentheses.


    Something like this:

    <?php
    function myTest($input) {
    $backtrace = debug_backtrace();
    $file = file($backtrace[0]['file']);
    $linenum = $backtrace[0]['line'];
    $function = $backtrace[0]['function'];
    $line = $file[$linenum-1];
    preg_match("/{$function}\((.*)\);/", $line, $matches);
    echo "This function called with '{$matches[1]}'";
    }

    $a = 5;
    myTest($a);

    ?>

+ Reply to Thread