Skip to content

Instantly share code, notes, and snippets.

@andho
Created January 7, 2011 15:09
Show Gist options
  • Save andho/769565 to your computer and use it in GitHub Desktop.
Save andho/769565 to your computer and use it in GitHub Desktop.
Demonstrating check for required fields with use of an array
public function __construct($data) {
$address = array('name', 'post_code');
$business = array('name', 'address', 'post_code', 'intro');
if ($data['type] == 'business') $fields = $business;
else $fields = $address;
if (count(array_intersect($fields, array_keys($data))) != count($fields)) {
throw new Exception("Required data was not supplied to the Address object");
}
$keys = array_intersect($fields, array_keys($data));
$data2 = array();
foreach ($keys as $key) {
$data2[$key] = $data[$key];
}
foreach ($data2 as $key=>$value) {
$method = 'set' . ucfirst($key);
$this->$method($value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment