Location: PHPKode > scripts > Object dynamic generation > object-dynamic-generation/model/Model.class.php
<?php
/*
 * Class Model
 * It dynamically creates object for its subclasses
 * @param protected $_fields
 * @author Roy
 */
abstract class Model {

	// This array holds the functions for subclasses
	protected $_fields = array();

	// Constructor doing nothing
	public function __construct() {}

	// It builds SETTER and GETTER for subclasses
	public function __call($method, $args) {
		
		if(preg_match("/set(.*)/",$method, $result)) {
			if(array_key_exists($result[1], $this->_fields)) {
				$this->_fields[$result[1]] = $args[0];
				return true;
			} else {
				$this->addMethods($result[1]);
				$this->_fields[$result[1]] = $args[0];
				return true;						
			}
		} else if(preg_match("/get(.*)/", $method, $result)) {
			if(array_key_exists($result[1], $this->_fields)) {
				return $this->_fields[$result[1]];
			}
		} 		
		return false;
	}
	
	/*
	 * This function add SETTER and GETTER into $fields
	 * @param $fields
	 */
	public function addMethods($fields) {
		if(!is_array($fields)) {
			$fields = array($fields);
		}
			
		foreach($fields as $key) {
			$this->_fields[$key] = null;
		}			
	}
}

?>
Return current item: Object dynamic generation