OOP is an abbreviation which stands for O bject O riented P rogramming.
While procedural programming involves writing procedures & functions that perform operation on data, object oriented programming entails the creation of objects that contains both the data & function.
Basically, A class is a template of an object
We create a class by using the class keyword, followed by the name of class and then curly braces
The class block of code, will be within the curly braces.
class class_Name {
//block of code goes in here
}
Usually classes contain properties and methods
A property is actually a variable inside a class.
A Method is actually a function inside a class.
An Object is basically an instance of a class
For example, let's consider a car as a class. Such a car has properties such as brand name, model, color, year of production, etc. An object of the car class will be Toyota because Toyota is an instance of a car that has all the properties of car such as model, color, and year of production.
Another object of the car class is Honda since it is an instance of a car.
With this in mind, we establish that an object of a class inherits all the properties and methods of the class.
To create an object in PHP, we use the new keyword, followed by the class name.
$object_Name = new class_Name;
Note that, multiple objects can be created from a class
Let's create a car class and create an object from it
<?php
class car{
//properties of the car class
public $brand_Name;
public $model;
public $color;
public $year_Of_Production;
//method, which will set the model of the car
public function set_Model($car_Model){
$this -> model = $car_Model;
}
//method which will return the brand name of the car
public function get_Car_Model(){
return $this -> model;
}
}
//we can now create an instance/object of the car class, say Honda
$honda = new Car;
//we now set the assign a value to the model property via the set_Model method parameter.
$honda -> set_Model("Accord");
//we can now output the car model
echo $honda -> get_Car_Model();
In the above example, 2 new items were introduced that is the $this and the -> , we will now look at these items and their functionality
In PHP, the $this , references the current object of the class. That is the $this keyword is used as a reference for all the yet to be created objects
The $this keyword is only available within the methods of a class
The -> is used to access a class property
The -> is also used by an Object to access or call a method
Let's look at a more practical example, most regular shapes such as rectangle, square, and triangle have certain numeric properties such as length and width, and these properties are be utilized in finding their areas, perimeters etc.
Let's now consider the regular shapes as a PHP class, and the numeric properties, as class properties, we will now use these properties to find the area and perimeter of the regular shapes, applying the apropriate formulars via class methods.
Take 9cm as the length of the rectangle, and 4cm as its width
<?php
class Regular_Shapes{
public $length;
public $width;
public function rectangle_Area($length, $width){
$this -> length = $length;
$this -> width = $width;
$area_Of_Rectangle = $this -> length * $this -> width;
return $area_Of_Rectangle;
}
public function rectangle_Perimeter($length, $width){
$this -> length = $length;
$this -> width = $width;
$perimeter_Of_Rectangle = (2 * $this -> length) + (2 * $this -> width);
return $perimeter_Of_Rectangle;
}
}
$rectangle = new Regular_Shapes;
echo "The area of the rectangle is ".$rectangle -> rectangle_Area(9, 4)."cm²";
//Supposing the length of the rectangle is now 2.6cm and the width is 5.8cm, find the perimeter
echo "<br> The perimeter of the rectangle is ".$rectangle -> rectangle_Perimeter(2.6, 5.8)."cm²";
Study the above tutorial carefully, and try to understand and practice the examples given.
Now applying the "code pattern" of the examples(without looking at them), and the knowledge you've gained so far, resolve the following:
1. Create a PHP class and via this class find the area of a triangle whose breadth is 7.5cm and whose height is 4cm. ( hint: area_Of_Triangle = 1/2 * breadth * height).
2. The area of a trapezium is given as ½ *(a + b)h, consider these variables as properties of a class, then find the area of a trapezium with the following parameters(a = 11, b = 13, h = 7).
There are many ways in which PHP classes can be applied, particularly in solving arithmetic problems. Go over Regular shapes and even trigonometry and try applying PHP classes and objects in resolving as many problems as possible. While this may have no practical applications in the ideal use of PHP classes, this will however get you acquinted and conversant with the basics, the syntaxes and logics , which will be very helpful when you begin to encounter real-life applications of classes.