Skip to content

Instantly share code, notes, and snippets.

@galvao
Last active January 2, 2016 09:09
Show Gist options
  • Select an option

  • Save galvao/8280931 to your computer and use it in GitHub Desktop.

Select an option

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
<?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);
}
}
}
}
@galvao
Copy link
Author

galvao commented Jan 7, 2014

@harikt Ah, of course, isset would work. Been a long day of work, missed that =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment