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.

Advertisements