Translated by Google Translate
Encapsulation, Inheritance, and Polimorfism in OOP PHP
A. Encapsulation
Encapsulation has correlation with visibility or hiding information, it means we create data that cannot be accessed except via method or function that we created. Encapsulation implementation used the function of setter and getter that I’ve ever told in the previous OOP article. In that article I made the tutorial about encapsulation when property and method in class is protected by keyword “private or protected”.
Look at the script below that I mention in the previous article. In this script I’ve implementing encapsulation :
![]() |
Figure 1 Implementing Encapsulation |
B. Inheritance
Inheritance means, we can create a new class use existing class, exist class called parent class and new class that created via parent class called child class. The purpose from inheritance is to reuse the code we have created with adding new property and method.
To create a new class that inherit characteristic from another class you need to using keyword extends. Here is the formula to creating child class using keyword extends.
child_class extends parent_class
First type the name of child_class and then following by keyword extends and the name of parent_class. Now, let’s create 2 simple class, they are class Parent and Child. Look at the picture below :
Script 1. Implementing Inheritance
<?php
class Parents {
public function helloParent() {
echo "This is parent class ...<br />";
}
}
class Child extends Parents {
public function helloChild() {
echo "This is child class ...<br />";
}
}
$objectChild = new Child();
$objectChild->helloParent();
$objectChild->helloChild();
?>
There are two class appears in browser even we just create one class that’s because class Child inheritance from class Parent. Now we will continue about visibility protected in the previous tutorial, let’s modify class Parent and Child in the script above by adding one protected property in class Parent with name $name and one method in class child with name printParentName(). So this is the script :
Script 2. Implementing Inheritance with Protected Visibility
<?php
class Parents {
protected $name = 'Leonardo Da Vinci';
public function helloParent() {
echo "This is parent class ...<br />";
}
}
class Child extends Parents {
public function helloChild() {
echo "This is child class ...<br />";
}
public function printParentName() {
echo "Parent Name : $this->name <br />";
}
}
$objectChild = new Child();
$objectChild->helloParent();
$objectChild->printParentName();
$objectChild->helloChild();
?>
Script 2. Implementing Inheritance with Protected Visibility
<?php
class Parents {
protected $name = 'Leonardo Da Vinci';
public function helloParent() {
echo "This is parent class ...<br />";
}
}
class Child extends Parents {
public function helloChild() {
echo "This is child class ...<br />";
}
public function printParentName() {
echo "Parent Name : $this->name <br />";
}
}
$objectChild = new Child();
$objectChild->helloParent();
$objectChild->printParentName();
$objectChild->helloChild();
?>
Property $name in parent class that using protected will run by method printParentName() in child class. When the script run then the name of parent class “Leonardo Da Vinci” will appear even the method run by child class. Look at the picture below :
![]() |
Figure 3 Implementing Inheritance using Protected Visibility |
It is proving that protected visibility can accessed via child class.
C. Polimorfism
Polimorfism is simple, in the previous tutorial you have used polimorfism. Polimorfism is a process creating one or more new object at some class. Let’s we reopen the script in previous tutorial, look at the script below :
Script 3. Implementing Polimorfism
<?php
class people {
private $name;
function __construct($name) {
$this->name=$name;
echo "Constructor : $this->name created <br>";
}
function speaking() {
echo "Hallo, my name is ".$this->name." <br />";
}
function __destruct() {
echo "Destructor : $this->name deleted <br/ >";
}
}
$people1 = new People("People 1");
$people1->speaking();
$people2 = new People("People 2");
$people2->speaking();
?>
<?php
class people {
private $name;
function __construct($name) {
$this->name=$name;
echo "Constructor : $this->name created <br>";
}
function speaking() {
echo "Hallo, my name is ".$this->name." <br />";
}
function __destruct() {
echo "Destructor : $this->name deleted <br/ >";
}
}
$people1 = new People("People 1");
$people1->speaking();
$people2 = new People("People 2");
$people2->speaking();
?>
Well, the text I colouring is polimorfism ....


Constructor and Destructor in OOP (PHP)
In OOP, you will never separate from constructor and destructor. What is constructor and destructor in OOP? Constructor is special statement (also called special method) that created when an object created (instance of class).
If you didn't create the constructor or destructor in a class then
constructor and destructor automatically created. When you create $objPeople in the previous OOP tutorial, then constructor and destructor automatically created
Normally,
the constructor is created to provide an initial operation to be
performed when an object created (inilization object). This is the
writting format how to create a constructor
function __constructor(parameter) { code } |
Keyword construct should started with double underscore and then give the parentheses (). Among the parentheses, you can give the parameter if you think you need to add parameter. Then you can build your code in constructor.
Eg you create class product, in that class include property of quantity that the initialize value shuld be 0, then the code will look like below :
class barang { private name; private price; private quantity; function __construct() { $this->quantity = 0; } } |
If the constructor created and used when the object created, then destructor is the reverse of constructor. Destructor created and used when the object deleted. In PHP programming, destructor automatically called in your final PHP scripting.
Same like constructor, writing format of destructor begin by double underscore. Here it is :
function __destructor(parameter) { code } |
And this another example of constroctor and destructor :
<?php class people { private $name; function __construct($name) { $this->name=$name; echo "Constructor : $this->name created <br>"; } function speaking() { echo "Hallo, my name is ".$this->name." <br />"; } function __destruct() { echo "Destructor : $this->name deleted <br/ >"; } } $people1 = new People("People 1"); $people1->speaking(); $people2 = new People("People 2"); $people2->speaking(); ?> |
And this is the result in browser :
In the picture above, there are six teks, the result from calling the constructor, calling method speaking(), and calling destructor. Though you don't put code call method __construct() & method __destruct(). This happen because when you create an object, it's automatically creating constructor for the object. Then when it has used, it will removed from memory. It's automatically the destructor from that object will be called.
Here is the example of the use constructor, destructor, method, object, and property for calculating the student's grade.
And the looks like in browser is :


Public, Private, and Protected (Member Visibility) in OOP PHP
In OOP we will always find keywords Public, Private, and Protected. These three things are used to determine the visibility of members (properties and functions) that are characteristic of object-oriented programming. Are members globally accessible (public) or accessed by its class (private) or accessed by the class and subclasses. Meanwhile, if the procedural programming members can only be accessed globally.
A. Public
In public visibility, a method or property can be accessed globally. That means it can be accessed on the inside and outside of class or in other words the properties and functions are known globally. In OOP PHP tutorial for Beginers previously, we have made $objPeople which the visibility of properties and function are public.
This is another example of class with public visibility :
Script 1.
<?php
class student {
public $sid;
public $name;
public $point;
public function processPoint () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>
class student {
public $sid;
public $name;
public $point;
public function processPoint () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>
And this is the result looks like in browser
![]() |
Picture 1. Public member visibility |
B. Private
If public can accessed by global then private visiblity means that the member (method or property) in a class are accessible only within its own class.
Let's modify the script at class student by changing the property visibility to be private.
Let's modify the script at class student by changing the property visibility to be private.
Script 2
<?php
class student {
private $sid;
private $name;
private $point;
public function processPoint () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>
class student {
private $sid;
private $name;
private $point;
public function processPoint () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>
If we run the script 2, it will display an error message because the property sid, name and points have private visibility, so that the data accessed by outside of class can not be done. So how do the private property that is accessible outside of class so that it can be used by objects that need it? The solution is to use the function setter and getter.
Setter is used to create a (set of) value for a property. This function is generally used for properties that have private and protected visibility. Setter is often used to validate incoming data before the data is loaded on a property. While the getter is used to generate (get) a value of the member public, private, or protected from the result.
How to access private member with that function?
Script 3
<?php
class student {
private $sid;
private $name;
private $point;
function setSid($x) {
$this->sid=$x;
}
function setName($x) {
$this->name=$x;
}
function setPoint($x) {
$this->point=$x;
}
function getSid() {
return $this->sid;
}
function getName() {
return $this->name;
}
function getPoint() {
return $this->point;
}
function Point () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->setSid('3310901051');
$objStudent->setName('Hermanto');
$objStudent->setPoint(90);
$objStudent->point();
?>
class student {
private $sid;
private $name;
private $point;
function setSid($x) {
$this->sid=$x;
}
function setName($x) {
$this->name=$x;
}
function setPoint($x) {
$this->point=$x;
}
function getSid() {
return $this->sid;
}
function getName() {
return $this->name;
}
function getPoint() {
return $this->point;
}
function Point () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->setSid('3310901051');
$objStudent->setName('Hermanto');
$objStudent->setPoint(90);
$objStudent->point();
?>
In the script above, we use function setters and getters for each property that has a private visibility. Making the value of the function by taking the value setter and getter done by function. In the getter is used "return" value because the getter will return the value of the property taken by the data.
Note how to calling the property after instancing $objStudent, calling the property is done by calling the names function with ($objStudent-> setSid()). Although become longer code lines, but more OOP recommend this method for accessing a Member.
If the script is run in the browser then the result will be the same as the picture below :
Note how to calling the property after instancing $objStudent, calling the property is done by calling the names function with ($objStudent-> setSid()). Although become longer code lines, but more OOP recommend this method for accessing a Member.
If the script is run in the browser then the result will be the same as the picture below :
![]() |
Picture 2. Calling private visibility with setter & getter function |
C. Protected
Protected almost SAME to the private that can only be accessed by its class and protected can also be accessed by its subclasses. Let's try changing the script 1 and we will change the visibility of the member be protected like the script below :Script 4
<?php
class student {
protected $sid;
protected $name;
protected $point;
public function processPoint () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>
class student {
protected $sid;
protected $name;
protected $point;
public function processPoint () {
echo "Students with sid $this->sid <br />";
echo "name $this->name <br />";
echo "get point $this->point";
}
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>
If the script is run in browser then the result will error : cannot access protected property.
The error occurred because the protected class can only be accessed by its class and subclasses. For more detailed discussion of inheritance protected in subclasses will be explained on the nexy tutorials on The Inheritance.


OOP Concept in PHP for Beginner
Object oriented programming has begun in era of PHP3, but still simple and not perfect. While web technologies and cloud computing grows up then PHP 5 has been developed to support full OOP. OOP become popular among web programmers. For example, many CMS like Joomla, WordPress, PrestaShop and Moodle are built with OOP. Furthermore, trend use of Frameworks like Yii Framework, Codelgniter, Zend, CakePHP and etc. All of them also built with OOP.
So, how to implement OOP in PHP? To answer this question, first you must have mindset when using OOP "Everything is an Object". Object is the "main actor" in OOP. So when you developing a website in PHP you should thinking about Object. You should think how interaction between obejcts can solve the problem.
Now, let's implementing OOP in PHP, There are four basic terms you should be known in OOP. They are Object, Class, Property and Method. And here they are :
A. Class
A class is a blueprint of a template according to which objects are created. Class is bearing of group element data (variable) and code program (function). Variable in class called property and function is method. To create a class in PHP, you can use the keyword "class" and then followed by class name. For an example here a class with name "People".
<?php
class people {
public $name;
public $age;
}
?>
In code above we create a class with name people (red) and its properties, name and age (green)
B. Method
Method in OOP can be defined as behavior or anything what can be done by object from a class. In PHP, method in class realized in function. Method can be called as anything what can be done by object. For an example, object people could have speaking, walking and agestatus. Here's the example of implementing method in class people.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
?>
in code above we create function named speaking and with content name and age.
C. Instance Object
Object is instance of class, so a class can't be used without object. To create an object in PHP we can use tag new and the code format is like this
$objectName = new className()
Look the code below, we will create an object for class people.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
?>
In code above we create an object instance from class people name $objpeople. When run in browser, it view nothing that's because the script not contain a command to doing something at object $objpeople.
D. Set Properties
After success instancing an object, now we'll setting the properties in object. In this case we'll setting the properties name and age. To setting the properties we can use :
$objectName->property = value;
Let's implement this to object people :
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
//VIEW THE PROPERTIES VALUE DI RROWSER
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age years old";
?>
At $objPeople->name = 'Herman'; We set the name value with "Herman" and $objPeople->age= '23'; We set the age value with "23". And to run that properties we use :
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age Tahun";
And this is the code looks like in browser :
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
//VIEW THE PROPERTIES VALUE DI RROWSER
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age years old";
?>
At $objPeople->name = 'Herman'; We set the name value with "Herman" and $objPeople->age= '23'; We set the age value with "23". And to run that properties we use :
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age Tahun";
And this is the code looks like in browser :
![]() |
Picure 1. Set Properties |
E. Running the Function
Here we try to call the function in PHP. In the previous case we create a funciton name speaking. Here's how to call that function.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
$objPeople->speaking();
?>
And how it's look like in browser:
![]() |
Picture 2. Call the Function |
To call the function we can use $objectName + "->" + functionName() + ";".
And that's all, thanks for coming, wait my next PHP tutorial.

