Pixel Everywhere

There are only 10 types of people in this world. Those who know binary and those who don't.

Expert OOP in PHP — October 15, 2010
Advanced OOP in PHP — October 14, 2010

Advanced OOP in PHP

This tutorial is the next step to combining and enhancing the concepts of the previous tutorial called Basic OOP in PHP. We will create a fictional company complete with departments, employees, conventions and algorithms.

Based on the previous tutorial, we created two classes called DepartmentType.php and Employee.php. This time, let’s create a class called Department.php and add a few methods to manipulate an employee class.

include_once 'Employee.php';

class Department{
	private var $aEmployees;
	private var $sType
	//
	public function __construct(){
		$this->setType("");
		$this->aEmployees = array();
	}
	//
	public function setType($type){
		$this->sType = $type;
	}
	public function getType(){
		return $this->sType;
	}
	public function addEmployee(Employee $employee){
		//Check for an existing employee.
		for($i = 0; $iaEmployees); $i++){
			if($this->aEmployees[$i] == $employee){
				return false;
			}
		}
		array_push($this->aEmployees, $employee);
		return true;
	}
	public function removeEmployee(Employee $employee){
		//Check for an existing employee.
		for($i = 0; $iaEmployees); $i++){
			if($this->aEmployees[$i] == $employee){
				array_splice($this->aEmployees, $i);
				return true;
			}
		}
		return false;
	}
	public function getEmployees(){
		//Return a clone to avoid modifying the original list.
		return array_slice($this->aEmployees);
	}
}

Let’s first break the code into small pieces.

public function __construct(){
	$this->setType("");
	$this->aEmployees = array();
}

Since this is a constructor, we initialize both the type and the list of employees.

To add a new employee, we need an instance of an employee type in the parameter.

public function addEmployee(Employee $employee){
	//Check for an existing employee.
	for($i = 0; $iaEmployees); $i++){
		if($this->aEmployees[$i] == $employee){
			return false;
		}
	}
	array_push($this->aEmployees, $employee);
	return true;
}

What is does is loop through a list of employees. If it does find a duplicate, the function automatically returns false, which would indicate an unsuccessful insertion. Otherwise, it adds the employee being passed in the parameter and returns true.

The next method is removing an employee based on the employee data type being passed.

public function removeEmployee(Employee $employee){
	//Check for an existing employee.
	for($i = 0; $iaEmployees); $i++){
		if($this->aEmployees[$i] == $employee){
			array_splice($this->aEmployees, $i);
			return true;
		}
	}
	return false;
}

What it does is almost the same with adding, however in reverse order. If it finds the specific requested employee in the list, it removes it and automatically returns true to indicate successful deletion. Otherwise, returns false.

To use it in our company.php file, we do it like this:

include_once 'Employee.php';
include_once 'DepartmentType.php';
include_once 'Department.php';
//
//Create a new employee.
$emp = new Employee();
$emp->setName("Shiela");
//
//Create a department with a specific type.
$dept = new Department();
$dept->setType(DepartmentType::IT);
//
//Add the newly created employee to the department's list.
$dept->addEmployee($employee);
//
//Loop through all the employees of the newly created department and display the name.
for($i = 0; $i getEmployees()); $i++){
	$emp = $dept->getEmployees()[$i];
	echo "$emp->getName() 
"; }

And there you have it. A company complete with one department and one employee. Hope you will get the hang of it and refer later on to the Expert OOP in PHP for the next tutorial that involves saving the content to a database.

Basic OOP in PHP —

Basic OOP in PHP

In this PHP tutorial, you will learn how to structure an object oriented PHP files that can be used in all of your PHP related projects. The tutorial will include concepts, naming conventions and basic algorithms that will help you create better and modularized projects.


What is OOP anyways and why should I use it?

This is the very simple structure of an object in PHP. This will serve as a template and anything you create will have the same initial layout like this.

<?php
class ClassName{
	public function __construct(){
	}
}
?>

The word “__construct” with two underscores at the front is called a constructor. We’ll discuss about constructors in the later part. For now, we must save the file with the same name as the class, which in this case is ClassName.php. Let’s assume that all classes are enclosed with the php declaration tags. From now on, all sample codes will imply that they’re enclosed by the php declaration tags.

Once the layout is done, it is now possible to add custom properties. Let us create a simple object called Employee. An employee is a typical person that is a member of our entire system. Let us imagine our entire system is a fictional company. A person normally in has a name, an id and a type of department where he typically works at.

class Employee{
	private var $sName;
	private var $iID;
	private var $sDepartmentType;
	public function __construct(){
	}
}

Once the properties are declared, we must create accessor methods called getters and setters. Methods are just functions inside a class. These accessor methods can set or return a specific value in which it is assigned to. Let’s create both getters and setters for all the properties.

class Employee{
	private var $sName;
	private var $iID;
	private var $sDepartmentType;
	public function __construct(){
		//Constructor.
	}
	//Getters.
	public function getName(){
		return $this->sName;
	}
	public function getID($id){
		return $this->iID;
	}
	public function getDepartmentType(){
		return $this->sDpeartmentType;
	}
	//Setters.
	public function setName($name){
		$this->sName = $name;
	}
	public function setID($id){
		$this->iID = $id;
	}
	public function setDepartmentType($departmentType){
		$this->sDepartmentType = $departmentType;
	}
}

We can then initialize their values with an empty strings for both name and department types while 0 for the id from the constructor.

public function __construct(){
	$this->setName("");
	$this->setID(0);
	$this->setDepartmentType("");
}

The purpose of a constructor is to renew all the properties. Imagine creating a reset method without creating one. A typical usage of an object in PHP is like this:

$employee = new Employee();

Wherein the word “Employee()” in the line “new Employee()” is the constructor.

Once everything is set up in the Employee.php class file, we can now create another class called DepartmentType.php, which consists different standard department types that are declared as constants.

class DepartmentType{
	const IT = "IT";
	const ENGINEERING = "Engineering";
	const ELECTRONIC_PUBLISHING = "Electronic Publishing";
	public function __construct(){
		//Constructor.
	}
}

Since we now have two classes, let’s now combine them together. In a non-class company.php file, we create a new instance of an employee, assigned to a specific department and display them into the browser.

include_once 'Employee.php';
include_once 'DepartmentType.php';

$angel = new Employee();
$angel->setName("Shiela");
$angel->setID(1);
$angel->setDepartmentType(DepartmentType::IT);

echo "$angel->getName() <br />";
echo "$angel->getID() <br />";
echo "$angel->getDepartmentType() <br />";

Those were your first objects in PHP. We’ve created a fictional company with different types of standard departments and an employee that belongs to that certain department. There is another tutorial called Advanced Object Oriented Programming that provides a wider view in OOP using the examples that were just created.