I have a very specific way I like to create my objects which seems to work very well.  I’ve ben using it for years now and it consistently does what I want my object to do: hold the logic of the object and allow me flexibility in the future for upgrades to ideas on how the object should work.

My objects are generally centered around the idea of a row in a MySQL database.  A job for instance would be one row in a database a a job object would represent that data plus all of the code necessary to make it function in the system I’m working on.

I generally make getVariable and setVariable type functions for each column in the database.  Technically yes, this is overkill, until your boss one day decides to append ad additional function when the value changes from X to Y on a certain column and your system now needs to do additional work on that concept.

Yes, there are more efficient methods of getting and setting data into databases, but I stand by this one and I believe it’s a highly re-usable concept that has served me extremely well over the years.

You’ll notice some functions in this object that are not well (or at all) defined here.  I’ll follow up with more posts in the future describing what they are, why they are, what they do, and some example code for each. You’ll also see that I reference a field called “id”. In all of my tables I always create an id field that is the primary key that auto-increments. Does this use more space? Yes. Does it make life a metric ton easier? Yes.

<?php

class job {
	var $id;
	var $data = array();
	var $tableName = "jobs";
	var $databaseName = "my_database_name";

	function __construct($params=array()) {
		if(!is_array($params)){
			$params = array('id'=>$params);
		}
		if(@$params['id'] > 0){
			$query = 'SELECT * FROM 
						`'.$this->databaseName.'`.`'.$this->tableName.'` 
						WHERE `id` = "'.encodeSQL($params['id']).'" 
						LIMIT 1';
		}
		if($query != NULL){
			$result = getMySQLData($query);
			if(mysql_num_rows($result) > 0){
				$this->data = @mysql_fetch_assoc($result);
				$this->id = $this->data['id'];
			}
		}
	}


	function getTitle() {
		return $this->data["title"];
	}

	function getIDhiringManager() {
		return $this->data["id_hiring_manager"];
	}
	
	function setTitle($input) {
		$this->data["title"] = $input;
	}

	function setIDhiringManager($input) {
		$this->data["id_hiring_manager"] = $input;
	}

	function dataChecks() {
	}
	
	function save() {
		$this->dataChecks();
		$this->id = objectSave(	$this->tableName, 
								$this->id, 
								$this->data, 
								$this->databaseName, 
								array('writeAudit'=>true)
								);
	}
	
	function duplicate() {
		$this->id = NULL;
		$this->data['id'] = NULL;
	}
	
	function delete() {
		objectDelete(	$this->tableName, 
						$this->id, 
						$this->databaseName, 
						array('writeAudit'=>true)
					);
	}
	
}

July 26th, 2013

Posted In: Objects

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: