Last active
January 2, 2016 09:09
-
-
Save galvao/8280931 to your computer and use it in GitHub Desktop.
Another way to write the exchangeArray method of the Model Class in ZF2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Please read the comments below in this Gist carefully as this approach has very important caveats. | |
| namespace Application\Model; | |
| class Foo | |
| { | |
| public $id; | |
| public $bar; | |
| public $baz; | |
| public $quux; | |
| public function exchangeArray($data) | |
| { | |
| $attributes = array_keys(get_class_vars(__CLASS__)); | |
| foreach($attributes as $attribute) { | |
| if (isset($data[$attribute])) { | |
| $this->$attribute = (!empty($data[$attribute]) ? $data[$attribute] : null); | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@harikt Ah, of course, isset would work. Been a long day of work, missed that =)