PHP Object, set some properties

Is it possible to set several properties at the same time for an object in php? Instead of this:

$object->prop1 = $something; $object->prop2 = $otherthing; $object->prop3 = $morethings; 

do something like:

 $object = (object) array( 'prop1' => $something, 'prop2' => $otherthing, 'prop3' => $morethings ); 

but without overwriting the object .

+7
source share
7 answers

Not like the way you want it. but this can be done using a loop.

 $map = array( 'prop1' => $something, 'prop2' => $otherthing, 'prop3' => $morethings ); foreach($map as $k => $v) $object->$k = $v; 

See only two additional lines.

+14
source

You should see Practical Guidelines for Object Oriented PHP :

"since setter functions return $ this, you can chain them as follows:"

  $object->setName('Bob') ->setHairColor('green') ->setAddress('someplace'); 

This, by the way, is known as a free, free interface .

+8
source

I would recommend you not to do this. Seriously not .

Your code is much MORE cleaner in the first way, it is more clear from your intentions, and you do not focus your code to such an extent that sometime in the future someone will look at your code and think: “What the hell was an idiot of thinking”?

If you insist on doing something that is clearly wrong, you can always create an array, iterate over it and set all the properties in a loop. However, I will not give you the code. This evil.

+4
source

You can write some setters for an object that returns an object:

 public function setSomething($something) { $this->something = $something; return $this; //this will return the current object } 

Then you can:

 $object->setSomething("something") ->setSomethingelse("somethingelse") ->setMoreThings("some more things"); 

You will need to write a setter for each property, since the __set function __set not able to return a value.

Alternatively, set up one function to take an array of properties => values ​​and set everything?

 public function setProperties($array) { foreach($array as $property => $value) { $this->{$property} = $value; } return $this; } 

and pass to the array:

 $object->setProperties(array('something' => 'someText', 'somethingElse' => 'more text', 'moreThings'=>'a lot more text')); 
+2
source

I really would not do this .... but for fun I would

 $object = (object) ($props + (array) $object); 

you end up with stdClass consisting of the public properties of $ objects, so it loses its type.

0
source

The objectThis() method objectThis() properties of the class array or array to stdClass. Using a direct transtypage (object) will remove the numerical index, but with this method it will contain the numerical index.

 public function objectThis($array = null) { if (!$array) { foreach ($this as $property_name => $property_values) { if (is_array($property_values) && !empty($property_values)) { $this->{$property_name} = $this->objectThis($property_values); } else if (is_array($property_values) && empty($property_values)) { $this->{$property_name} = new stdClass(); } } } else { $object = new stdClass(); foreach ($array as $index => $values) { if (is_array($values) && empty($values)) { $object->{$index} = new stdClass(); } else if (is_array($values)) { $object->{$index} = $this->objectThis($values); } else if (is_object($values)) { $object->{$index} = $this->objectThis($values); } else { $object->{$index} = $values; } } return $object; } } 
0
source

I understand that this is an old question, but for the benefit of others who encounter it, I decided this myself recently and wanted to share the results

 <?php //Just some setup header('Content-Type: text/plain'); $account = (object) array( 'email' => 'foo', 'dob'=>((object)array( 'day'=>1, 'month'=>1, 'year'=>((object)array('century'=>1900,'decade'=>0)) )) ); var_dump($account); echo "\n\n==============\n\n"; //The functions function &getObjRef(&$obj,$prop) { return $obj->{$prop}; } function updateObjFromArray(&$obj,$array){ foreach ($array as $key=>$value) { if(!is_array($value)) $obj->{$key} = $value; else{ $ref = getObjRef($obj,$key); updateObjFromArray($ref,$value); } } } //Test updateObjFromArray($account,array( 'id' => '123', 'email' => ' user@domain.com ', 'dob'=>array( 'day'=>19, 'month'=>11, 'year'=>array('century'=>1900,'decade'=>80) ) )); var_dump($account); 

Obviously, there are no built-in protections. The main caveat is that the updateObjFromArray function assumes that for any nested arrays inside $array corresponding key in $obj already exists and is an object, this should be true or treat it as an object, it will throw an error.

Hope this helps! :)

0
source

All Articles