One of the simple and very useful pieces of code in Pherret (a PHP 5 framework) is the setProperties() function in the base Object class. The name originally came from Java Beans and works in a similar manner. I’m sharing it because it could be used in any code with PHP 5 or PHP 4. First, here is the problem being solved. Do you ever find yourself setting and object (or array, or variables) from a HTTP post in the following manner?

 $user = new User(); $user->firstName = $_POST['firstName']; $user->lastName = $_POST['lastName']; $user->address = $_POST['address']; $user->city = $_POST['city']; $user->state = $_POST['state']; $user->zip = $_POST['zip']; $user->email = $_POST['email']; // validate the data $user->saveToDatabase(); 

Don’t you think it would be easier to do this?

 $user = new User(); $user->setProperties(); // validate data $user->saveToDatabase(); 

This can be done by having the User class extend the Object class. The Object class has a method called setProperties($params = $_REQUEST, $properties = '*', $prefix = '', $force = false) which by default sets all the properties that an extending object has from the $_REQUEST. There are many other ways you can use this method. You can set another source to populate your object from, including $_GET, $_POST, another array or even another object. You may also limit which properties are set by passing them into the $properties parameter in a comma delimited list. To set all properties you pass '*'. To set only some you would pass 'prop1, prop2, prop3.' If you want _all_ the properties set except one or two you may add a negative sign in front of the property like '*, -prop1, -prop2', '*' says use all, then the '-prop1,-prop2' would exclude those properties. In addition you may add a $prefix to the properties. This is useful if you have two objects and want to set both properties this way (ie. firstName, shipFirstName, billLastName). It takes care of the camel-case for you. And finally, there is the $force parameter. The default is false which means that only properties currently in the object will be set from items in the $params array/obj. By setting $force to true you may force all items to be set on the object. This is useful when you have an empty object, or one that has no class definition. Now, here is what you’ve been waiting for. The setProperties method to add to any class (or even better, any parent class).

 function setProperties($params = null, $properties = "*", $prefix = "", $force = false) { if (is_object($params)) { $params = get_object_vars($params); } elseif (empty($params)) { $params = $_REQUEST; } elseif (!is_array($params)) { //throw new Exception('The $params sent to setProperties is niether an object or an array (and it should be)'); return; } $props = explode(",", str_replace(" ", "", $properties)); $properties = array(); foreach ($props as $prop) { if ($prop == '*') { $properties = $force ? $params : get_object_vars($this); } elseif ($prop{0} == '-') { unset($properties[substr($prop, 1)]); } else { $properties[$prop] = 1; } } foreach ($properties as $prop => $value) { $prop = ($prop{0} == '_' ? substr($prop, 1) : $prop); 
The of different in lead turn amaryl when the lower: Do can as allergies back Want body. For and to your around and much even people typically. Them results diovan there exact episodes cycling Monti.
$param = empty($prefix) ? $prop : $prefix . ucfirst($prop); if (array_key_exists($param, $params)) { $setter = "set" . ucfirst($prop); $this->$setter($params[$param]); } } } function getProperties() { return get_object_vars($this); }

I threw in the bonus getProperties() function :) I hope this can save at least one developer some repetative coding in their application.