473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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


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
9 1979
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
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
..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
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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Xavier | last post by:
Greetings, I was wondering if there was a way to get a list of all the variables which were executed/created/modified within a function? Here is a simple example that comes to my mind: def...
1
by: Yasutaka Ito | last post by:
Hi, Is there a way to programmatically find out the caller of your function? For example, let's say I have a function called MyFunction(), and I want to debug print the caller of this function...
10
by: Dixie | last post by:
I need to delete some relationships in code. How do I know what the names of those relationships are?
3
by: Tor Inge Rislaa | last post by:
Finding name and type In the activate procedure of a form I want to write to the debug window, name and type of all controls at that actual form. Is there a smart way to do that? Allso for...
6
by: Tor Inge Rislaa | last post by:
Finding name of all forms I want to create a procedure that can loop trough all objects of type form in an application and print the name property to the debug window. TIRislaa
12
by: Bill Moran | last post by:
Hey all. I've hit an SQL problem that I'm a bit mystified by. I have two different questions regarding this problem: why? and how do I work around it? The following query: SELECT GCP.id,...
4
by: RedJoy | last post by:
I am switching from VS2003 .NET to VS2005 and I receive the following errorwhen compiling my project: error C3821: 'File': managed type or function cannot be used in an unmanaged function...
2
by: Maxwell_Smart | last post by:
Is there a way for a function to refer to itself generically? I'd like to use such a thing (should it exist) for convenience and consistency, not functionality. For example: Function...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.