2

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

3 Answers 3

3
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);
?>
Sign up to request clarification or add additional context in comments.

9 Comments

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
Yeah, I should known you didn't have to do anything special. PHP is very lose. Thanks guys.
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.
|
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.