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.

Advertisements