Posts Tagged ‘Object Oriented Programming’

Object Oriented Programming (OOP) and PHP

When I first started university I knew a bit of PHP and managed to mackle together lines of procedural code to preform basic functionality on a website such as add, edit, delete and view data within a MySQL database. Within my 2nd year I was introduced to a book (PHP Objects, Patterns and Practise) that changed my life.

I studied it chapter to chapter until I got bored and decided to actually try to do something with my new found knowledge and started writing very basic Object Oriented PHP using the poorly implemented PHP4 OOP syntax. I slowly grasped the concepts and pieced together a basic framework to increase my development time and stop me from constantly rewriting the same code over and over again.

Most of my developments came from writing my first online application which is a productitiy tool we use to manage all our clients, jobs, quotes, and passwords. The main benefit at this level was the use of a database and paging class which proves to be a ball ake for anyone that still writes procedural PHP.

After grasping the concepts I quickly moved over to PHP5s OOP syntax which has a far greater support for the Object Oriented approach. The move from OOP in PHP4 to PHP5 is about  the same level as moving from procedural programming to Object Oriented Programming with hordes of new functionality that should be expected of any OOP language.

Personally I actually find Object Oriented Programming easer than Procedural its the move between the two that causes problems with the method of working being a completely different approach.

OOP revolves around data rather than actions much the opposite to Procedural. A common example is the following:

<?php
class books{
	//private means can only be accessed from within the class
	private $title;
	private $author;
	private $isbn;
	private $year;

	//the __construct function is called as soon as the object is created.
	//Public means the method can be accessed from anywhere inside or outside the class
	public function __construct($title, $author, $isbn, $year){
		//$this refers to this class (books). In this case it is referring to private $title; found in line 4
		$this->title = $title;
		$this->author = $author
		$this->isbn = $isbn;
		$this->year = $year;
	}

	//notice how you do not need to pass $this->title into the method
	public function checktitle(){
		//check the book title only contains 0-9 a-z (uppercase or lowercase) hyphens, underscores and spaces
		if (eregi("([a-z0-9-_ ]+)", $this->title))
			{
				return true;
			}
		return 0;
	}

	public function checkauthor(){
		//check the book author only contains 0-9 a-z (uppercase or lowercase) hyphens, underscores and spaces
		if (eregi("([a-z0-9-_]+)", $this->author))
			{
				return true;
			}
		return 0;
	}

	public function checkisbn(){
		//check the book author only contains 0-9 and is 13 characters long
		if (eregi("([0-9]{13})", $this->isbn))
			{
				return true;
			}
		return 0;
	}

	public function checkyear(){
		//check the book author only contains 0-9 and is 4 characters long
		if (eregi("([0-9]{4})", $this->year))
			{
				return true;
			}
		return 0;
	}
	//run all the validation checks return true if all pass
	public function validate(){
		if (!$this->checktitle()){
			return false;
		}
		if (!$this->checkauthor()){
			return false;
		}
		if (!$this->checkisbn()){
			return false;
		}
		if (!$this->checkyear()){
			return false;
		}
		return true;
	}

	public function save(){
		//save the file into the database
	}
	//this is the last thing to run on the script
	public function __destruct(){
		unset($this->title, $this->author, $this->isbn, $this->year);
	}
}
//create an object $books
$books = new books('PHP Objects, Patterns, &amp; Practice', 'Matt Zandstra', '9781590599099', '2008');

//this will return a fatal error becase we are trying to echo a private object variable
echo $books->title;
//notice how we are using $books->title rather than $this->title because we are outside the class

//check if the title is valid
if ($books->checktitle()){
		echo 'This book title is valid'; //does not echo because the title contains , and &amp;
	}
//check if the author is valid
if ($books->checkauthor()){
		echo 'This book author is valid'; //this will echo because the string contains no illegal characters
	}
//save the data into the database if all the validation is passed
if ($books->validate()){
	$books->save();
}
//now the script has finished running __destruct will be called and unset all the object variables
?>

This is a very basic class and the books idea is a very common analogy when teaching Object Oriented Programming. Whenever you want to add a new book and check that the data is valid you can simply create a new object with the following code:

<?php
$books = new books('my title', 'my author', 'isbn', 'my year');
//if passes validation save into database
if ($books->validate()){
$books->save();
}
?>

If you were to want to preform the same function with Procedural PHP you would need to rewrite all the same code again. This takes time effort and in a commercial environment money.

Hopefully what I have written will help you grasp the concepts of Object Oriented Programming with PHP5 and pull you away from your Procedural habits.

I hope to expand on this and slowly start to show the true potential of OOP in Online Application Development.

Useful Links and Resources:

  • Share/Bookmark