In this tutorial you will learn oops concept in php, object oriented programming in php, how to create php class and object.
Before you start learning oops concepts, make sure you read my earlier tutorial for class, objects, properties in php, which will help you to understand oops concept better.
How to write object oriented programming in php!
First we learn Four pillar of oops Inheritance, Abstraction, Encapsulation, Polymorphism, access modifiers in PHP
Let’s look at example of all four pillar of oops implementation in php programming.
In Php we can achieve Inheritance using extends
keyword. let’s look at the example below
First we create a base class car
<?php class car { public $model="Not Set"; public $color="Not Set"; public $price=0.0; private $id; public function set_model($model_name) { $this->model = $model_name; } public function get_model() { return $this->model; } } ?>
Now we create the child class Maruti
, which will inherit from base class car using extends keyword
<?php class Maruti extends car { public $chassis=00000; } ?>
Now if we create a new instance of Maruti class $maruti = new Maruti();
,
we will be able to access all public properties of base class car print_r($maruti->get_model());
Abstract class and properties in php start with abstract
keyword, here is an example of Abstract class
<?php abstract class DesignCar { // Force Extending class to define this method abstract protected function getMileage(); abstract protected function fuelType($fule); // Common method public function printOut() { print $this->getMileage(); } } ?>
Now if we inherit from above abstract class, we must implement all abstract method defined inside the abstract class, let’s see an example!
<?php class Maruti extends DesignCar { public $chassis=00000; public function getMileage() { return "100 KM"; } protected function fuelType($fule) { return "Petrol"; } } $maruti = new Maruti(); echo $maruti->getMileage(); ?>
Notice, in above example , we have inherited from abstract class class Maruti extends DesignCar
Then we have implemented the abstract method.
public function getMileage() {
return "100 KM";
}
One more thing to notice in that example that we could access the method outside the class Maruti
, because during implementation we have changed the access modifier to public
and calling the method of new instance Maruti $maruti->getMileage();
What is Encapsulation in OOPs? Encapsulation is a concept of wrapping data into single unit, which is safe to deal with external object or component.
Let's see some example of encapsulation in Php
<?php class car { public $model="Not Set"; protected $VIN="000000"; public $price=0.0; private $id; } ?>
Now if you in above car class we have four variables, but all variables are not accessible outside the class, which variable, property, method to be accessed outside the class will be controlled from inside by using right access modifiers.
Like private $id;
can be used only inside the class, and protected $VIN="000000";
is available to class that will inherit from this car class. Similarly we can create properties, method, function and encapsulate them within that class.
You may be interested to read following posts: