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/
============================ 9 1963
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
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.
..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
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.
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/
============================
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/
============================
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/
============================
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
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/
============================ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Dixie |
last post by:
I need to delete some relationships in code. How do I know what the names
of those relationships are?
|
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...
|
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
|
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,...
|
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...
|
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...
|
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){
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |