tags:

views:

123

answers:

3

Is it possible to define attributes to a class in the constructor. For instance I pass a Associative Array to a class constructor and I want the attributes to that class to be declared and set based on what is in the Associative Array.

TIA

A: 

Yes, you can do that. off top of my head

public function __constructor($data){
  foreach($data as $k=>$v){
     $this->{$k} = $v;
  }
}

This should do it, and of course you need to math define the fields.

I m not too sure if it s {$k} or ${k} or ${$k} but either of these should work.

+2  A: 
class Foo{
   function __construct($arr){
       foreach($arr as $k => $v)
          $this->$k = $v;
   }
}

You can review the constructor/desctructor manual and properties manual.

to be noted since you don't define the properties in the class all are set to public which IMHO is kind of dangerous. I think it might be possible to achieve the same thing using the reflection. I just checked more in depth the reflection and it is not possible (with PHP5), since it would make sense to be able to do that from the reflection it might come with PHP6.

full sample

<?php
class Foo{
   function __construct($arr){
       foreach($arr as $k => $v)
          $this->$k = $v;
   }

   function getBar(){
       return $this->bar;
   }
}

$bar = new Foo(array(
    'bar' => 'bar',
    'foo' => 'foo'
)
);

var_dump($bar->bar);
?>
RageZ
I m not sure if he means this.
you are right I have edited but I think there is a function to do that. I am searching now
RageZ
$this->$k= $v would die.
Yeah, I should known you didn't have to do anything special. PHP is very lose.Thanks guys.
Jeremiah
what you need to search is how to execute a variable output as string :) I have actually done this, and many frameworks uses this.
ok seems there is a `get_class_vars` but not the reverse so that's the only way.
RageZ
`$this->$k= $v` is fine ... let's try
RageZ
did you try that?
give a try ... I have edited the answer with a full snippet
RageZ
your solution also work. I don't really know if it make kind of any difference.
RageZ
reflection in php is really nasty :) I m suprised $this->$k worked. :) the difference is your answer is picked. not mine :)
actually the funny things is if the prop have some space you have to write `$a->{'foo bar'}`
RageZ
A: 

You can certainly do this, as RageZ describes above, but I don't think I would recommend doing it. What this does is creates too loose of a "contract" between the users of this class - i.e. nobody really knows which properties the class has.

Instead of defining the properties on the fly, I would bet that you have a pre-defined set of "object property sets" that could in turn define a class hierarchy.

So instead of doing

  • if I get an array with "a = 2 and b = 3", create object with $obj->a = 3 and $obj-b = 3
  • if I get an array with "c = 5", create an object with $obj->c = 5

Do this:

  • define a class for MyObjectOfTypeOne, that has properties $a and $b
  • define a class for MyObjectOfTypeTwo that has property $c

Then, use the Factory pattern to generate the right type of object depending on the parameters passed to the static factory method.

Alex