473,385 Members | 1,400 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,385 software developers and data experts.

Getting the names of variables passed to functions

Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:

<?php
function foo($bar)
{
return $bar;
}

$name = foo($variable_name);
?>

I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.

Oct 10 '07 #1
8 1653
BoneIdol wrote:
Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:

<?php
function foo($bar)
{
return $bar;
}

$name = foo($variable_name);
?>

I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.
Out of interest, why do you want to do this?

If there isn't a PHP function (there is get_defined_vars() but I don't
think this does what you want) then you could create your own class that
manages variables.

eg.

class CVar {
protected $var_name = '';
protected $var_value = '';
public function __construct( $name = '', $value = '' ) {
$this->var_name = $name;
$this->var_value = $value;
}
public function getName() { return $this->name; }
public function getValue() { return $this->value; }
public function setName($name) { $this->var_name = $name; }
public function setValue($value){ $this->var_value = $value; }
}

function foo($bar) {
return $bat->getName();
}

$myvar = new CVar('animal','dog');
echo foo( &$myvar );
OR something like that....

just curious why ;-)

.... and now someone will point a really easy way to do it and as well
and i'll look a fool... LOL
Oct 10 '07 #2
Tyno Gendo wrote:
OR something like that....

just curious why ;-)

... and now someone will point a really easy way to do it and as well
and i'll look a fool... LOL
I noticed a few typos after I'd typed it into my email, cut and paste
into nusphere to see if it ran... so here is adjusted version with
corrected var names... I'm still curious.. why ? ;-)

class CVar {
protected $var_name = '';
protected $var_value = '';
public function __construct( $name = '', $value = '' ) {
$this->var_name = $name;
$this->var_value = $value;
}
public function getName() { return $this->var_name; }
public function getValue() { return $this->var_value; }
public function setName($name) { $this->var_name = $name; }
public function setValue($value){ $this->var_value = $value; }
}

function foo($bar) {
return $bar->getName();
}

$myvar = new CVar('animal','dog');
echo foo( $myvar );
Oct 10 '07 #3
On 10 Oct, 13:39, Tyno Gendo <tyno.ge...@example.netwrote:
BoneIdol wrote:
Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:
<?php
function foo($bar)
{
return $bar;
}
$name = foo($variable_name);
?>
I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.

Out of interest, why do you want to do this?

If there isn't a PHP function (there is get_defined_vars() but I don't
think this does what you want) then you could create your own class that
manages variables.

eg.

class CVar {
protected $var_name = '';
protected $var_value = '';
public function __construct( $name = '', $value = '' ) {
$this->var_name = $name;
$this->var_value = $value;
}
public function getName() { return $this->name; }
public function getValue() { return $this->value; }
public function setName($name) { $this->var_name = $name; }
public function setValue($value){ $this->var_value = $value; }
}

function foo($bar) {
return $bat->getName();
}

$myvar = new CVar('animal','dog');
echo foo( &$myvar );

OR something like that....

just curious why ;-)

... and now someone will point a really easy way to do it and as well
and i'll look a fool... LOL
It's more of a thought experiment than anything else. The idea is to
be able to define variables in classes on the fly with method
overloading. (function __get etc.)

So something like...

class foo
{
public var $bar;
private var $_vars = array();

public function __get($var)
{
$varname = get_variable_name($var); //Whatever code I need here
$_vars[$varname] = $var;
}
}

Note I just made that up off the top of my head and it's not finished
and doesn't let you work with variables that have already been
defined.

Really I'm just trying to do it to see if I can. ;)

Oct 10 '07 #4
BoneIdol wrote:
On 10 Oct, 13:39, Tyno Gendo <tyno.ge...@example.netwrote:
>BoneIdol wrote:
>>Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:
<?php
function foo($bar)
{
return $bar;
}
$name = foo($variable_name);
?>
I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.
Out of interest, why do you want to do this?

If there isn't a PHP function (there is get_defined_vars() but I don't
think this does what you want) then you could create your own class that
manages variables.

eg.

class CVar {
protected $var_name = '';
protected $var_value = '';
public function __construct( $name = '', $value = '' ) {
$this->var_name = $name;
$this->var_value = $value;
}
public function getName() { return $this->name; }
public function getValue() { return $this->value; }
public function setName($name) { $this->var_name = $name; }
public function setValue($value){ $this->var_value = $value; }
}

function foo($bar) {
return $bat->getName();
}

$myvar = new CVar('animal','dog');
echo foo( &$myvar );

OR something like that....

just curious why ;-)

... and now someone will point a really easy way to do it and as well
and i'll look a fool... LOL

It's more of a thought experiment than anything else. The idea is to
be able to define variables in classes on the fly with method
overloading. (function __get etc.)

So something like...

class foo
{
public var $bar;
private var $_vars = array();

public function __get($var)
{
$varname = get_variable_name($var); //Whatever code I need here
$_vars[$varname] = $var;
}
}

Note I just made that up off the top of my head and it's not finished
and doesn't let you work with variables that have already been
defined.

Really I'm just trying to do it to see if I can. ;)
I see. A sort of 'variables factory'. well, if you mix the class CVar
and your 'fooFactory' you could have a result.
Oct 10 '07 #5
On Oct 10, 8:49 am, BoneIdol <leon...@hotmail.comwrote:
On 10 Oct, 13:39, Tyno Gendo <tyno.ge...@example.netwrote:
BoneIdol wrote:
Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:
<?php
function foo($bar)
{
return $bar;
}
$name = foo($variable_name);
?>
I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.
Out of interest, why do you want to do this?
If there isn't a PHP function (there is get_defined_vars() but I don't
think this does what you want) then you could create your own class that
manages variables.
eg.
class CVar {
protected $var_name = '';
protected $var_value = '';
public function __construct( $name = '', $value = '' ) {
$this->var_name = $name;
$this->var_value = $value;
}
public function getName() { return $this->name; }
public function getValue() { return $this->value; }
public function setName($name) { $this->var_name = $name; }
public function setValue($value){ $this->var_value = $value; }
}
function foo($bar) {
return $bat->getName();
}
$myvar = new CVar('animal','dog');
echo foo( &$myvar );
OR something like that....
just curious why ;-)
... and now someone will point a really easy way to do it and as well
and i'll look a fool... LOL

It's more of a thought experiment than anything else. The idea is to
be able to define variables in classes on the fly with method
overloading. (function __get etc.)

So something like...

class foo
{
public var $bar;
private var $_vars = array();

public function __get($var)
{
$varname = get_variable_name($var); //Whatever code I need here
$_vars[$varname] = $var;
}

}

Note I just made that up off the top of my head and it's not finished
and doesn't let you work with variables that have already been
defined.

Really I'm just trying to do it to see if I can. ;)
You don't need this to "define variables in classes on the fly with
method overloading." That's exactly what __get() and __set() are
for. I'm not sure why you need the name of the variable that was
passed to it. In your example above you're using __get() to do what
is supposed to be done with __set().

Don't you really just want something like this:

class Foo {

private $theVars = array();

public function __get($name) {
return $this->theVars[$name];
}

public function __set($name, $val) {
$this->theVars[$name] = $val;
}
}

Oct 10 '07 #6
On 10 Oct, 14:18, ZeldorBlat <zeldorb...@gmail.comwrote:
On Oct 10, 8:49 am, BoneIdol <leon...@hotmail.comwrote:
On 10 Oct, 13:39, Tyno Gendo <tyno.ge...@example.netwrote:
BoneIdol wrote:
Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:
<?php
function foo($bar)
{
return $bar;
}
$name = foo($variable_name);
?>
I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.
Out of interest, why do you want to do this?
If there isn't a PHP function (there is get_defined_vars() but I don't
think this does what you want) then you could create your own class that
manages variables.
eg.
class CVar {
protected $var_name = '';
protected $var_value = '';
public function __construct( $name = '', $value = '' ) {
$this->var_name = $name;
$this->var_value = $value;
}
public function getName() { return $this->name; }
public function getValue() { return $this->value; }
public function setName($name) { $this->var_name = $name; }
public function setValue($value){ $this->var_value = $value; }
}
function foo($bar) {
return $bat->getName();
}
$myvar = new CVar('animal','dog');
echo foo( &$myvar );
OR something like that....
just curious why ;-)
... and now someone will point a really easy way to do it and as well
and i'll look a fool... LOL
It's more of a thought experiment than anything else. The idea is to
be able to define variables in classes on the fly with method
overloading. (function __get etc.)
So something like...
class foo
{
public var $bar;
private var $_vars = array();
public function __get($var)
{
$varname = get_variable_name($var); //Whatever code I need here
$_vars[$varname] = $var;
}
}
Note I just made that up off the top of my head and it's not finished
and doesn't let you work with variables that have already been
defined.
Really I'm just trying to do it to see if I can. ;)

You don't need this to "define variables in classes on the fly with
method overloading." That's exactly what __get() and __set() are
for. I'm not sure why you need the name of the variable that was
passed to it. In your example above you're using __get() to do what
is supposed to be done with __set().

Don't you really just want something like this:

class Foo {

private $theVars = array();

public function __get($name) {
return $this->theVars[$name];
}

public function __set($name, $val) {
$this->theVars[$name] = $val;
}

}
Heh, looking back I forgot my $this->s. Oops.

Being honest I only just started looking into more advanced class
handling today, including overloading. Wish the php.net examples were
a bit clearer, wouldn't look like such an idiot now. ;)

Oh well, kept me entertained for a little while at least.

Oct 10 '07 #7
On Oct 10, 2:00 pm, BoneIdol <leon...@hotmail.comwrote:
Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:

<?php
function foo($bar)
{
return $bar;
}

$name = foo($variable_name);
?>

I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.
Okay if you are using classes. Then this is dead on easy.

<?php

class MyClass {
var $myname;
var $myemail;
var $myphonenr;
function getVariable($var){ return $this->$var;}
function setVariable($var,$value){ $this->$var = $value;}
}

// Example use;
$myclass = new $MyClass();

$myclass->setVaribale("myname","Michael");
$myclass->setVaribale("myemail","mi*****@greenquery.com") ;

print "Hi " . $myclass->getVariable("myname");

//View the class
var_dump($myclass);
?>

I hope this is what your looking for

Best Regards
Michael

Oct 10 '07 #8
On Oct 10, 7:53 pm, mich...@greenquery.com wrote:
On Oct 10, 2:00 pm, BoneIdol <leon...@hotmail.comwrote:
Anyway to do it? I know you can use a variable's contents as a
variable name with $$name. With something like this:
<?php
function foo($bar)
{
return $bar;
}
$name = foo($variable_name);
?>
I'd like the function foo to return a string of the variable name
passed to it, in this case 'variable_name'. A friend of mine who does
C ++ programming says that pointers are the way to go here,
but as far as I know PHP doesn't support them.

Okay if you are using classes. Then this is dead on easy.

<?php

class MyClass {
var $myname;
var $myemail;
var $myphonenr;
function getVariable($var){ return $this->$var;}
function setVariable($var,$value){ $this->$var = $value;}
}

// Example use;
$myclass = new $MyClass();

$myclass->setVaribale("myname","Michael");
$myclass->setVaribale("myemail","mich...@greenquery.com") ;

print "Hi " . $myclass->getVariable("myname");

//View the class
var_dump($myclass);

?>

I hope this is what your looking for

Best Regards
Michael
// Sorry Just fixed my misspells!
Okay if you are using classes. Then this is dead on easy.

<?php

class MyClass {
var $myname;
var $myemail;
var $myphonenr;
function getVariable($var){ return $this->$var;}
function setVariable($var,$value){ $this->$var = $value;}
}

// Example use;
$myclass = new $MyClass();

$myclass->setVariable("myname","Michael");
$myclass->setVariable("myemail","mich...@greenquery.com") ;

print "Hi " . $myclass->getVariable("myname");

//View the class
var_dump($myclass);

?>

I hope this is what your looking for

Best Regards
Michael

Oct 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
46
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names....
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
10
by: tony | last post by:
i'm trying to itterate through an array that contains the names of the global arrays eg: $myarray = array("\$_GET", "\$_SERVER"); and so on The problem Im having is calling a function...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
10
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.