1

Possible Duplicate:
PHP - Initialize object members with array parameter

I am looking for a way to have my class apply data from an associative array to its own member variables. However, if those variables do not exist, the process should not throw any errors.

Please note: i am either not looking for what the extract() function does or i misunderstand its behaviour.

Example:

class customer {

    public $firstName;
    public $lastName;
    public $email;

    public function apply($data){
        /* here i want to be able to iterate through $data and set the class
           variables according to the keys, but without causing problems 
           (and without creating the variable in the class) if it does not exist
           (yet)) */
    }

}

$c=new customer();
$c->apply(array(
    'firstName' => 'Max',
    'lastName'  => 'Earthman',
    'email'     => '[email protected]'
));

Hope the description is proper.

Please ask if anything is unclear.

4
  • Any reason why you don't want to catch the exception for a missing attribute? Commented Oct 7, 2012 at 16:29
  • Honestly, i just felt like there must be a more elegant way and i was looking for that. Commented Oct 7, 2012 at 16:30
  • why don't you use a constructor method? Commented Oct 7, 2012 at 16:33
  • hakre: Thanks for the info! Of course its the instance we talk about. Darshan: i use a constructor but for different things - in this example i cut it down to the bare necessities. Commented Oct 7, 2012 at 16:34

2 Answers 2

2

You can do something like this

class customer {
    public $firstName;
    public $lastName;
    public $email;

    public function apply($data) {
        $var = get_class_vars(__CLASS__);
        foreach ( $data as $key => $value ) {
            if (array_key_exists($key, $var)) {
                $this->$key = $value;
            }
        }
    }
}

$c = new customer();
$c->apply(array('firstName' => 'Max','lastName' => 'Earthman','email' => '[email protected]',"aaa" => "bbb"));
var_dump($c);

Output

object(customer)[1]
  public 'firstName' => string 'Max' (length=3)
  public 'lastName' => string 'Earthman' (length=8)
  public 'email' => string '[email protected]' (length=16)
Sign up to request clarification or add additional context in comments.

4 Comments

Baba, you're too fast answering.
@hakre ..........lol ...lol .............. lol ....
That looks very promising, but am i correctly assuming that if $this->$key is not existant, it will be created and $value will be applied to it? Because i would like to avoid that. Before you edited your question you had an isset() condition included, why was that removed - is it not working?
Looks promising! Will be tested in a minute.
0
//Works for private as of 5.3.0 
//Will also return statics though.

public function apply($data){
   foreach($data  as $key => $val) {
      if(property_exists('customer',$key)) {
         $this->$key = $val;
      }
   } 
}

If this is coming from a database there are functions to return the object directly rather than having to apply the associated array to a class. Just a thought.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.