Getting the names of variables passed to functions

Getting the names of variables passed to functions

am 10.10.2007 14:00:42 von BoneIdol

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

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.

Re: Getting the names of variables passed to functions

am 10.10.2007 14:39:12 von Tyno Gendo

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

Re: Getting the names of variables passed to functions

am 10.10.2007 14:44:21 von Tyno Gendo

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

Re: Getting the names of variables passed to functions

am 10.10.2007 14:49:14 von BoneIdol

On 10 Oct, 13:39, Tyno Gendo wrote:
> 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:
>
> > > > 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. ;)

Re: Getting the names of variables passed to functions

am 10.10.2007 15:15:17 von Tyno Gendo

BoneIdol wrote:
> On 10 Oct, 13:39, Tyno Gendo wrote:
>> 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:
>>> >>> 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.

Re: Getting the names of variables passed to functions

am 10.10.2007 15:18:58 von zeldorblat

On Oct 10, 8:49 am, BoneIdol wrote:
> On 10 Oct, 13:39, Tyno Gendo wrote:
>
>
>
> > 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:
>
> > > > > > 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;
}
}

Re: Getting the names of variables passed to functions

am 10.10.2007 15:35:31 von BoneIdol

On 10 Oct, 14:18, ZeldorBlat wrote:
> On Oct 10, 8:49 am, BoneIdol wrote:
>
>
>
> > On 10 Oct, 13:39, Tyno Gendo wrote:
>
> > > 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:
>
> > > > > > > > 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.

Re: Getting the names of variables passed to functions

am 10.10.2007 15:40:50 von Tyno Gendo

BoneIdol wrote:
> On 10 Oct, 14:18, ZeldorBlat wrote:
>> On Oct 10, 8:49 am, BoneIdol wrote:
>>
>>
>>
>>> On 10 Oct, 13:39, Tyno Gendo wrote:
>>>> 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:
>>>>> >>>>> 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.
>

Join the club, I'm trying to learn proper class/pattern usage myself but
there don't appear to be too many easy to understand or full-blown
examples that I could find... I bought a Java Patterns book instead and
started re-working them for PHP instead.

I've still to create a really good class orientated site, I often start
with classes, don't document anything, forget about the stuff I've done
and end up inline coding most of the stuff i want by the end of the
project :-/

Think i need to spent more time designing and look up uml etc./document
stuff better before I begin. hehe.

Re: Getting the names of variables passed to functions

am 10.10.2007 19:53:52 von Michael

On Oct 10, 2:00 pm, 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:
>
> > 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.


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","michael@greenquery.com");

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

//View the class
var_dump($myclass);


?>

I hope this is what your looking for

Best Regards
Michael

Re: Getting the names of variables passed to functions

am 10.10.2007 19:55:34 von Michael

On Oct 10, 7:53 pm, mich...@greenquery.com wrote:
> On Oct 10, 2:00 pm, 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:
>
> > > > 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.
>
> >
> 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.


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