An MVC Framework
An MVC Framework

The Model Class


The Model class will be the base class for data objects and business logic. All data object classes will be derived from the Model class and you will add your business logic to those derived classes.

For the purist or academic, the Model object is not a true data object. It is a hybrid. While the actual business logic is encapsulated in the Model object derivatives, the data of those objects is not. Instead the Model object serves as a proxy or receptacle for the intangible, unrealized data object. The actual data of the data object is stored in a member which is a generic container.

There are two types of data in a Model object: intrinsic and extrinsic data. Intrinsic data is data about the actual memory object and is stored in member variables. Extrinsic data is the represented intangible object's data and is stored in a DataMap. (A DataMap is an key-value pair associative array.) The reasons for this will become apparent.

	class Model {
		
		private $state = 'new';		// intrinsic data
		private $clean = true;
			
		private $data = array();	// extrinsic data stored in associative array(s)
		private $changed = array();
		
		⋮
	}	

Setters and Getters

Every data object needs a way of getting data in and out, hence Setters and Getters. Intrinsic data should be protected and only manipulated by the Model class. Therefore, Setters and Getters are only implemented for extrinsic data.

I'll illustrate the Setter and you can deduce the Getter from there.

Rather than define a Setter for every variable (as done in some languages/frameworks), I've implemented a generic Setter 'setData()':

	class Model {
	
		⋮
		
		function setData( $varname, $value, ... ) {	// $varname is the variable or field name of the data
		
			$this->data[$varname] = $value;
			$this->changed[$varname] = $value;
			$this->clean = false;
		}
		
		⋮
	}	

Notice that in addition to setting the main DataMap, I've also added the changed data to another datamap called 'changed' and set the clean flag to false indicating that this object is now dirty and needs to be saved.

Simple enough. Straight forward. — Fraught with peril? ... Almost.

Now since our input is an HTTP request we need a convenient way to load the incoming data. Here I define setDataFromRequest().

	class Model {
	
		⋮
		
		function setDataFromRequest( $indata ) {	// typically $indata will be $_GET or $_POST
			
			foreach( $indata as $key => $value ) {
				
				$this->setData($key, html_entity_decode($value) );
	
			}
		}
					
		⋮
	}	

Now what has just happened is, in a few lines of code, we have a mechanism to take data from an HTTP request (GET or POST) and populate our DataMap. The best part is that we currently don't know how many or what type the data fields are and we don't care. This function can handle anything we throw at it.

This is what I was referring to by lightweight and letting me program in the abstract. On a Request you merely call $DataObject->setDataFromRequest($_POST) and you move on to other more interesting things.

And in a perfect world where we can trust the data from the request we would be done. But it's not a perfect world and we can't trust the data.

So there's the Peril! Blindly accepting data from a request and inputing it into our object is a recipe for disaster. Not to worry, it all gets cleaned up soon enough.



Back to Top
Top

© 2012 and beyond Lawrence L Hovind - All Rights Reserved