You are here: Re: Getting the names of variables passed to functions « PHP Programming Language « IT news, forums, messages
Re: Getting the names of variables passed to functions

Posted by BoneIdol on 10/10/07 12:49

On 10 Oct, 13:39, Tyno Gendo <tyno.ge...@example.net> 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:
>
> > <?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. ;)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация