Jump to content

Archived

This topic is now archived and is closed to further replies.

hvle

question

Recommended Posts

i have function:

function func(&$var)
{
echo "the name of variable passed is: ";
// echo the name of variable passed to the function.
}

for example, when I have:

$myhome = "This is my home";

//call
func($myhome);

// would print out:
"the name of variable passed is: myhome"

Is this possible guys?
is to get the name of the variable passed to a function, maybe reference or copy, doesn't matter.


Share this post


Link to post
Share on other sites
I don't think it is possible because the arguments passed to a function can be variables or constants.

Share this post


Link to post
Share on other sites
i think so too,

but look at how Session handled this issue.

session_register($somevar);

and you can recall this $somevar in the session array:
$_SESSION['somevar']

Share this post


Link to post
Share on other sites
you could echo the variable name in single quotes

echo '$variable'

this would echo out

$variable

and i guess do a preg_replace or something to chop off the $.

Share this post


Link to post
Share on other sites
[!--quoteo(post=387358:date=Jun 24 2006, 04:21 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 24 2006, 04:21 AM) [snapback]387358[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i think so too,

but look at how Session handled this issue.

session_register($somevar);

and you can recall this $somevar in the session array:
$_SESSION['somevar']
[/quote]
No that is not how sessions handle it or anyother function in php. It doesnt create a session var [b]somevar[/b] if your variable is called $somevar. It creates the session variable based on the [b]value[/b] of $somevar. So if you had this:
[code]// assign $somevar the value of 'hello'
$somevar = "hello";
session_resgister($somevar);[/code]
It'll create a session variable based on the [b]value[/b] of [i]$somevar[/i] which will be [b]hello[/b] and so the variable created is this: [i]$_SESSION['hello'][/i]

It is not chopping of the dollor sign or anythink like that, it replaces the variable with its value. Thats how variables work.

Share this post


Link to post
Share on other sites
I can't chop off the $ sign cuz I do not know what the variable name is. (the variable passed to a function).

in the case of session:

$somevar = 'hello';
session_register('somevar');

echo $_SESSION['somevar']; // will echo hello

in this case, session_register magicly know the name of variable $somevar and bind this $_SESSION['somevar'] to the value of $somevar.

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]session_register magic know the name of variable $somevar[/quote]
No... you are wrong. Read your post again, even it doesn't explain things this way.

Share this post


Link to post
Share on other sites
this is copy straight from manual.
I do not prefer session_register(), but I like to be able to do something similar to it.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
session_register() accepts a variable number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays.

// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");
[/quote]

it's deprecated however.

The deal is how it take a string 'barney' literately, then actually search the script for a variable with that name.

Perhap all variables in main (script) stored in some sort of array. If so, I want to find this magical array.

Share this post


Link to post
Share on other sites
All variables are stored in the superglobal array $_GLOBALS

If you do a [code]<?php echo '<pre>' . print_r($_GLOBALS,true) . '</pre>'; ?>[/code] after defining a number of variables, you can see how they are stored.

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=387661:date=Jun 25 2006, 02:26 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 25 2006, 02:26 PM) [snapback]387661[/snapback][/div][div class=\'quotemain\'][!--quotec--]
All variables are stored in the superglobal array $_GLOBALS

If you do a [code]<?php echo '<pre>' . print_r($_GLOBALS,true) . '</pre>'; ?>[/code] after defining a number of variables, you can see how they are stored.

Ken
[/quote]


You're right Ken, anyway, it's $GLOBALS.
They should name it $_GLOBALS to be consistent with other global vars thought.
Thank you.

Share this post


Link to post
Share on other sites
maybe if you explain why you are trying to do this, we can help you find a satisfactory solution.

Share this post


Link to post
Share on other sites
Yes of course.

I'm writing a class to parse template. This class called SimpleTemplate.
The class works great, and i'm trying to improve it.

you have a template file in html like this:

<html>
<body>
Hello {{name}}!
</body>
</html>

you have an array like this:
$bundle['name'] = "Jessica";


The SimpleTemplate use this template and array, i.e $bundle and parse the value Jessica with the {{name}}.

So, instead of define an array like this:
$name = "Jessica";
$bundle['name'] = $name;

I will improve it like this:

$st = new SimpleTemplate();
$st->register('name');

this register function will automaticly create an item 'name' in the member variable and assign the value of $name.

Well, that's the idea.
Otherwise, the class work great. I know there're ton of class out there with similar purpose, but I prefer my own class, cuz I know exactly how well it perform. Efficiency is always important to me.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.