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();
?>

 If that script run, then this is looks like in browser :
Figure 2 Implementing Inheritance
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();
?>

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();

?>

Well, the text I colouring is polimorfism ....



Read More Add your Comment


 

©2013 @namakuherman | Developed by Herman Creative Industry