By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

Finding the name of a variable used in the caller of a function fromwithin the function itself.

P: n/a

Hi all,

Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?

Or to be more clear:

...php code..
$result = foo($abc);
...more phpcode..
$result = foo($def);
function foo($aVar){
// Can I find out somehow here that $aVar was $abc or $def?
}

I can of course change the function and also pass the name of the used
variable, but that is not very convenient.

Regards,
Erwin Moller
--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 11 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Erwin Moller schreef:
Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?
You could try something like the following:

function foo(&$aVar){
$org = $aVar;
$varName = 'unknown';
$aVar = "UNIQUE_$aVar";
foreach ($GLOBALS as $k =$v) {
if ($v === $aVar) {
$varName = $k;
break;
}
}
$aVar = $org;
print "Varname is $varName";
}
JW
Sep 11 '08 #2

P: n/a
On Thu, 11 Sep 2008 12:38:56 +0200, Janwillem Borleffs wrote:
Erwin Moller schreef:
>Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?
foreach ($GLOBALS as $k =$v) {
That is a clever solution, but only works when the variable has global
scope. I don't think it is possible to find the variable name in the
calling function from within PHP. That said, you could get the file and
line number of the calling function with debug_backtrace() and then peek
into that file.

Sep 11 '08 #3

P: n/a
..oO(Erwin Moller)
>Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?
May I ask why you need this? Usually all that matters is the variable's
value. In many other languages you won't even be able to determine the
name, since it only exists in the sources, but not in the compiled code.

Micha
Sep 11 '08 #4

P: n/a
Erwin Moller wrote:
>
Hi all,

Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?

Or to be more clear:

..php code..
$result = foo($abc);
..more phpcode..
$result = foo($def);
function foo($aVar){
// Can I find out somehow here that $aVar was $abc or $def?
}

I can of course change the function and also pass the name of the used
variable, but that is not very convenient.

Regards,
Erwin Moller

Why would you ever want to do that? It goes against all the principles
of module isolation and limited scope. It is a throwback to the old
COBOL and BASIC days where everything was global in nature.

A function should be totally stand-alone in scope and whatever it needs
from the outside world (outside of that function) should be passed into
it via the argument list. (Granted, there may be a few application wide
settings that are global). If you need the name of the variable doing
the calling (why would one ever need that?), then by all means pass it
in as a text string. Otherwise, it is only its value that is important
in the function.
Sep 11 '08 #5

P: n/a

sheldonlg schreef:
Erwin Moller wrote:
>>
Hi all,

Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?

Or to be more clear:

..php code..
$result = foo($abc);
..more phpcode..
$result = foo($def);
function foo($aVar){
// Can I find out somehow here that $aVar was $abc or $def?
}

I can of course change the function and also pass the name of the used
variable, but that is not very convenient.

Regards,
Erwin Moller


Why would you ever want to do that? It goes against all the principles
of module isolation and limited scope. It is a throwback to the old
COBOL and BASIC days where everything was global in nature.

A function should be totally stand-alone in scope and whatever it needs
from the outside world (outside of that function) should be passed into
it via the argument list. (Granted, there may be a few application wide
settings that are global). If you need the name of the variable doing
the calling (why would one ever need that?), then by all means pass it
in as a text string. Otherwise, it is only its value that is important
in the function.
Hi sheldonlg (and Michael too),

Valid comment, I know it is a strange question.

Why I ask this?
Curiosity mostly.

Why I need it?
Well, for convenience.

In this case: I noticed I typed a LOT of the following code during
development:

echo "POST=<pre>";
print_r($_POST);
echo "</pre>";

or something similar. (eg $_FILES, $_SESSION, $foo, etc)

So I made this function (which I only use during development/debugging):

// debugfunction
function doprintr($someArr){
echo "<pre>";
print_r($someArr);
echo "</pre>";
}

So now I only have to type:
doprintr($_SESSION);

I thought it would be nice in that little helperfunction could produce
the name of the array it is formatting/printing.

Then I started thinking how and could figure out how, hence my question.

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 11 '08 #6

P: n/a

Michael Fesser schreef:
.oO(Erwin Moller)
>Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?

May I ask why you need this? Usually all that matters is the variable's
value. In many other languages you won't even be able to determine the
name, since it only exists in the sources, but not in the compiled code.

Micha
Hi Micha,

See my answer to sheldonlg who wondered too why I need such a strange
contraption. (short answer: curiosity mostly.)

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 11 '08 #7

P: n/a

Erwin Moller (MrTypo) schreef:
Then I started thinking how and could figure out how, hence my question.
could is couldn't of course. ;-)

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 11 '08 #8

P: n/a
Erwin Moller wrote:
>
sheldonlg schreef:
>Erwin Moller wrote:
>>>
Hi all,

Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?

Or to be more clear:

..php code..
$result = foo($abc);
..more phpcode..
$result = foo($def);
function foo($aVar){
// Can I find out somehow here that $aVar was $abc or $def?
}

I can of course change the function and also pass the name of the
used variable, but that is not very convenient.

Regards,
Erwin Moller


Why would you ever want to do that? It goes against all the
principles of module isolation and limited scope. It is a throwback
to the old COBOL and BASIC days where everything was global in nature.

A function should be totally stand-alone in scope and whatever it
needs from the outside world (outside of that function) should be
passed into it via the argument list. (Granted, there may be a few
application wide settings that are global). If you need the name of
the variable doing the calling (why would one ever need that?), then
by all means pass it in as a text string. Otherwise, it is only its
value that is important in the function.

Hi sheldonlg (and Michael too),

Valid comment, I know it is a strange question.

Why I ask this?
Curiosity mostly.

Why I need it?
Well, for convenience.

In this case: I noticed I typed a LOT of the following code during
development:
Yes, I can't count how many times I've done the same debugging. :)
echo "POST=<pre>";
print_r($_POST);
echo "</pre>";

or something similar. (eg $_FILES, $_SESSION, $foo, etc)

So I made this function (which I only use during development/debugging):

// debugfunction
function doprintr($someArr){
echo "<pre>";
print_r($someArr);
echo "</pre>";
}

So now I only have to type:
doprintr($_SESSION);

I thought it would be nice in that little helperfunction could produce
the name of the array it is formatting/printing.

Then I started thinking how and could figure out how, hence my question.

Regards,
Erwin Moller
This is an easier solution that changes your approach a bit. Instead
of passing the variable itself, why not just pass the variable name:

<?php
function do_printr($var_name) {
if ( isset($GLOBALS[$var_name]) ) {
echo "<pre>\n$var_name = ";
print_r($GLOBALS[$var_name]);
echo "</pre>\n";
}
}
?>

This works for any var in the global scope, so vars inside the
function itself, other functions, or classes wouldn't be visible.
However, it will *always* work for the superglobals. You could add a
second argument, which would accept an array of defined vars from
another scope (see: get_defined_vars()).

--
Curtis
Sep 11 '08 #9

P: n/a

Curtis schreef:
Erwin Moller wrote:
>>
sheldonlg schreef:
>>Erwin Moller wrote:

Hi all,

Is it possible (PHP5.2) to find the name of a variable used in the
caller of a function from within the function itself?

Or to be more clear:

..php code..
$result = foo($abc);
..more phpcode..
$result = foo($def);
function foo($aVar){
// Can I find out somehow here that $aVar was $abc or $def?
}

I can of course change the function and also pass the name of the
used variable, but that is not very convenient.

Regards,
Erwin Moller

Why would you ever want to do that? It goes against all the
principles of module isolation and limited scope. It is a throwback
to the old COBOL and BASIC days where everything was global in nature.

A function should be totally stand-alone in scope and whatever it
needs from the outside world (outside of that function) should be
passed into it via the argument list. (Granted, there may be a few
application wide settings that are global). If you need the name of
the variable doing the calling (why would one ever need that?), then
by all means pass it in as a text string. Otherwise, it is only its
value that is important in the function.

Hi sheldonlg (and Michael too),

Valid comment, I know it is a strange question.

Why I ask this?
Curiosity mostly.

Why I need it?
Well, for convenience.

In this case: I noticed I typed a LOT of the following code during
development:

Yes, I can't count how many times I've done the same debugging. :)
>echo "POST=<pre>";
print_r($_POST);
echo "</pre>";

or something similar. (eg $_FILES, $_SESSION, $foo, etc)

So I made this function (which I only use during development/debugging):

// debugfunction
function doprintr($someArr){
echo "<pre>";
print_r($someArr);
echo "</pre>";
}

So now I only have to type:
doprintr($_SESSION);

I thought it would be nice in that little helperfunction could produce
the name of the array it is formatting/printing.

Then I started thinking how and could figure out how, hence my question.

Regards,
Erwin Moller

This is an easier solution that changes your approach a bit. Instead of
passing the variable itself, why not just pass the variable name:

<?php
function do_printr($var_name) {
if ( isset($GLOBALS[$var_name]) ) {
echo "<pre>\n$var_name = ";
print_r($GLOBALS[$var_name]);
echo "</pre>\n";
}
}
?>

This works for any var in the global scope, so vars inside the function
itself, other functions, or classes wouldn't be visible. However, it
will *always* work for the superglobals. You could add a second
argument, which would accept an array of defined vars from another scope
(see: get_defined_vars()).
Hi Curtis,

Thanks for your reply.
Yes, we PHP programmers need a lot of those echo/pre/print_r thingies. ;-)
I considered your approaches too (varname or second argument.)

I was merely asking because I was curious if there was a way to find the
name of the argument in the caller from within the funtion itself.
Seems that is indeed difficult to do in a general way.
Allthough Sjoerd's suggestion may work in all situations (backtrace+
open file, find linenumber, and disect the code.): It is a little
cumbersome for such a little thing as my lazyness. ;-)

Thanks all for your suggestions.

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 12 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.