Re: Re: How to Set Object Array Value || In $this->$name[] Format...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It may be easier to do:

$array = $this->$name;
$array[] = $value;

Some combination of {} and $this->{$name}[] = $value may also work...

As it stands now, $name[] looks like you are trying to get an index on
$name FIRST, and then use $this-> on that SECOND...

I think.

On Thu, October 5, 2006 11:42 am, sit1way wrote:
> Hey all.
>
> I've got a simple object method that sets object vars based on a
> passed
> array:
>
> **********************************************************
> function set($arr) {
>
>     foreach($arr as $name => $value) {
>
>         if(is_array($this->name)) {
>               $this->$name[] = $value;
>         }
>         else {
>               $this->$name = $value;
>        }
>    }
> }
> *********************************************************
>
> Getting a fatal error when I include the file that contains this
> method:
>
> Fatal error: Cannot use [] for reading in path/to/my/file on line X
>
> Looks like PHP does not like the syntax of "$this->$name[]".
>
> Any clues how to set object array value in a loop?
>
> TIA,
>
> --Noah
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux