Jump to content

question


hvle

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.


Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.