Re: $myobject->$$varname doens't work ??
| From: | Aidan Lister | Date: | Sun, 09 May 2004 08:38:14 +0000 |
| Subject: | Re: $myobject->$$varname doens't work ?? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185734@lists.php.net to get a copy of this message | ||
"Greg" <news.php.net@gb404.com> wrote in message
news:20040509071800.74440.qmail@pb1.pair.com...
> Hello,
>
> I was just trying this but it doesn't work :
> <?php
>
> class a {
> public $foo = "hello world";
> }
>
> $obj = new a();
> $varname = "foo";
> echo $obj->$$varname;
>
$$varname is turned into $foo, what is the value of $foo?
If you're trying to access "public $foo" then Richard was entirely correct
(sorry richard!), just do it like so: $a->foo
If, like in the below example, $foo is the name of a variable that you want
to access, then try ${$foo}, everynow and then there's a problem to that
effect
Good luck, welcome to oop :)
> $bar = "this is working";
> $varname = "bar";
> echo $$varname; // display "this is working" as expected
> ?>
>
> Is it a bug or is it normal ?
> I didn't find anything about it in documentation (php4 / php5).
>
> Greg