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

From: Date: Thu, 05 Oct 2006 19:09:19 +0000
Subject: Re: Re: How to Set Object Array Value || In $this->$name[] Format...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-242706@lists.php.net to get a copy of this message
On 10/5/06, sit1way <sit1way@hotmail.com> 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?
I think the problem is the use of the dollar sign before the property name. Try this instead: [code]
        if(is_array($this->name)) {
              $this->name[] = $value;
        }
        else...
[/code] HTH, John W
TIA, --Noah -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#242706) next »