Employee Contacts (Part 4) – Building the Business Object Layer with PHP

  1. Building the MySQL database.
  2. Building a Data Abstraction Layer with PHP
  3. Building the Data Access Layer with PHP
  4. Building the Business Object Layer with PHP
  5. Building the Presentation Layer with PHP & HTML

Part 1 of this series covered building the MySQL database. In Part2, we built the data abstraction layer. With Part 3, we built our data access layer. At the data access layer, we made generated the queries to execute through the data abstraction layer. Part 4 of our series will cover the business object layer.

With the business layer, often times it will seem like we are simply making calls to the data access layer. And yes, that is true but remember that we are planning for future growth. A little extra time now can save you hours later down the road, and lead to far less headaches. As you will see when we build our presentation level, we should never make a direct call to the data access layer, those calls should be handled through our business layer.

Before we begin, you will need to create a new directory to hold all of your business objects. I create mine of the root of the web directory and call it business_objects. In this directory, create a file called employees.bo.php. Just in case you are wondering, the bo lets me know that this is a business object.

Defining our Class – First we need to define our business layer class. I choose to call mine bo_employees.

// we need to reference our data access class
require_once('./data_objects/employees.do.php');

// business object class
class bo_employees{
	// variable for the employee data object
	var $do_employees;
}

Class Constructor – We need to create our class constructor. Anything inside this method will be executed whenever we instantiate our class. The variables that we defined previously will now be set.

// class constructor
function bo_employees(){
	$this->do_employees = new do_employees();
}

Change Name Case – Realistically we would put this in another location so that we could call it from any method we needed it, but for our purposes I am placing it here. All this method does is verify that only the first letter of the first name is capitalized. Everything else should be lower case. You could get far more sophisticated and check to see if the person has 2 first names and verify that only the first letter of each name is capitalized. Or if the last name has a `’` in it, then the letter directly after it should be capitalized as well.

I did a project one time where the admin refused to use proper casing of names, so everything was in capital letters. It was very difficult to look at, so we had to create function to handle this. That project was with classic asp, so we can’t use the same one here, but this will work.

/**
* ensures only the first letter is capitalized.
*
* @param string $value
* @return string
*/
function change_name_case($value){
	// change the first letter to uppercase, then the remainder to lowercase
	$return = strtoupper(substr($value,0,1)) . strtolower(substr($value,1));
	return $return;
}

Select All Employees – We will call this method from the presentation layer whenever we want to display all employees.

/**
* returns all employees
*
* @param string $sort[optional]
* @return array
*/
public function get_all_employees($sort='last_name ASC'){
	$results = $this->do_employees->get_all_employees($sort);
	return $results;
}

Select One Employee – We will call this method from the presentation layer whenever we want to display a single employee. If we were to fully develop this application, there would be much more employee information available. Using the get_all_employees method would return an overview of the data available. When we select a single employee, this method would provide you with far more information. Probably providing contact information, addresses, managers name, etc.

/**
* returns an employees details
*
* @param int $empId
* @return array
*/
public function get_one_employee($empId){
	$results = $this->do_employees->get_one_employee($empId);
	return $results;
}

Add New Employee – We will call this method whenever we add a new employee. The purpose of the business layer is to implement business rules. That is why we added the change_name_case function and we will use it when adding a new employee. We could do that at display time, but the problem with that is that we will incur much more overhead. You will not be adding employees all the time, but people will be viewing the employees list on a daily basis.

/**
* add a new employee
*
* @param array $employee
*/
function add_new_employee($employee){
	$firstname = $this->change_name_case($employee["first_name"]);
	$employee["first_name"] = $firstname;
	$this->do_employees->add_employee($employee);
}

Edit Existing Employee – We call this method from the presentation layer whenever we are editing an employee. Once again we want to ensure that the first name only has 1 capital letter in it. You could call another method here that would alert the admin or the employee whenever there contact information is changed. This would allow the employee to verify the information was correct or would notify the admin that someone has been editing employee contact information.

/**
* update an existing employee
*
* @param array $employee
*/
function edit_existing_employee($employee){
	$firstname = $this->change_name_case($employee["first_name"]);
	$employee["first_name"] = $firstname;
	$this->do_employees->edit_employee($employee);
}

Delete Employee – This is called whenever we want to delete an employee. Something you could add here as a business rule would be to check if the user requesting the delete has access to do so. If they don’t then alert the administrator.

/**
* delete an employee
*
* @param int $empId;
*/
function remove_employee($empId){
	$this->do_employees->remove_employee($empId);
}

Complete Code

require_once('./data_objects/employees.do.php');

class bo_employees{
	// variable for the employee data object
	var $do_employees;

	// class constructor
	function bo_employees(){
		$this->do_employees = new do_employees();
	}

	/**
	* ensures only the first letter is capitalized.
	*
	* @param string $value
	* @return string
	*/
	function change_name_case($value){
		// change the first letter to uppercase, then the remainder to lowercase
		$return = strtoupper(substr($value,0,1)) . strtolower(substr($value,1));
		return $return;
	}

	/**
	 * returns all employees
	 *
	 * @param string $sort[optional]
	 * @return array
	 */
	function get_all_employees($sort='last_name ASC'){
		$results = $this->do_employees->get_all_employees($sort);
		return $results;
	}

	/**
	* returns an employees details
	*
	* @param int $empId
	* @return array
	*/
	public function get_one_employee($empId){
		$results = $this->do_employees->get_one_employee($empId);
		return $results;
	}

	/**
	* add a new employee
	*
	* @param array $employee
	*/
	function add_new_employee($employee){
		$firstname = $this->change_name_case($employee["first_name"]);
		$employee["first_name"] = $firstname;
		$this->do_employees->add_employee($employee);
	}

	/**
	* update an existing employee
	*
	* @param array $employee
	*/
	function edit_existing_employee($employee){
		$firstname = $this->change_name_case($employee["first_name"]);
		$employee["first_name"] = $firstname;
		$this->do_employees->edit_employee($employee);
	}

	/**
	* delete an employee
	*
	* @param int $empId;
	*/
	function remove_employee($empId){
		$this->do_employees->remove_employee($empId);
	}
}

No comments yet

Leave a reply