Encapsulation is a method or concept of object-oriented programming to wrap members or data or components or properties and methods or functions into one unit in a Class, and secure both from external interference or misuse.
Encapsulation is also called "data hiding", which means hiding the internal details of an object, such as details about the operations or how an object works.
The implementation of encapsulation usually uses 3 types of access rights:
- Public
- Protected
- Private
1. Public Access Rights
Allows a property or method to be accessed anywhere, whether in its own class, in its derived class, or in another class. Example:
Step One:
I created a Student class, and I saved it with the file name "MahasiswaA.php". I set all its properties and methods to Public.
<?php
/*
*bundet.com
*Wawan Beneran
*Mengenal Enkapsulasi - OOP - PHP
*/
class Mahasiswa{
public $nim;
public $nama;
public $nilai;
public function __construct($nim, $nama, $nilai)
{
$this->nim = $nim;
$this->nama = $nama;
$this->nilai = $nilai;
}
public function NamaMhs()
{
return $this->nama;
}
public function StatusNilai()
{
$status = ($this->nilai >= 60)? "Lulus" : "Gagal";
return $status;
}
}
?>
Step Two:
I created the MataKuliah class as a derivative of the Mahasiswa class, and I saved it with the file name "MatakuliahA.php". The extends statement is used to declare a derived class.
<?php
/*
*bundet.com
*Wawan Beneran
*Mengenal Inheritance - Enkapsulasi - OOP - PHP
*/
include "MahasiswaA.php";
class MataKuliah extends Mahasiswa{
public $matakuliah;
public $dosen;
public $status;
public function Matkul($matkul, $dosen){
$this->matakuliah = $matkul;
$this->dosen = $dosen;
}
// Mengambil properti dari class turunan
public function NilaiMhs(){
return $this->nilai;
}
public function Grade()
{
if ($this->nilai >= 70){
$grade = "A";
} elseif ($this->nilai >= 60){
$grade = "B";
} elseif ($this->nilai >= 50){
$grade = "C";
} elseif ($this->nilai >= 40){
$grade = "D";
} else{
$grade = "E";
}
return $grade;
}
public function StatusNilaiMhs()
{
$this->status = $this->StatusNilai();
$this->status;
}
}
?>
Step Three:
I created a special file to display the data details, and I saved it with the file name "data_mahasiswaA.php".
<?php
/*
*gatewan.com
*Wawan Beneran
*Mengenal Output - Inheritance - Enkapsulasi - OOP - PHP
*/
include "MatakuliahA.php";
$datanya = new MataKuliah(54321, "Mr.Bean", 88);
//echo "<h1>Matukuliahnya</h1>";
echo "NIM : " .$datanya->nim;
echo "<br />";
echo "Nama : " .$datanya->nama;
echo "<br />";
$datanya->Matkul("Pemrograman Web Dinamis", "Wahyu Widodo");
echo "Matakuliah : " .$datanya->matakuliah;
echo "<br />";
echo "Dosen : " .$datanya->dosen;
echo "<br />";
echo "Nilai : " .$datanya->nilai;
echo "<br />";
echo "Grade : " .$datanya->Grade();
echo "<br />";
echo "Status : " .$datanya->StatusNilai();
echo "<br />";
?>
Step Four:
Run the data_mahasiswaA.php file. This is the output:
The point is that all properties and methods can be accessed anywhere
2. Protected Access Rights
Allows a property or method to be accessed only by its own class and/or its derived classes. Example:
Step One:
Still using the same source code, as above, only, you need to change the access rights for the value property to protected.
<?php
class Mahasiswa{
public $nim;
public $nama;
protected $nilai; //Rubah hak akses $nilai menjadi protected
...
}
?>
When the property is changed to protected, then you run the file "data_mahasiswaA.php", then the following error will occur:
Notification Error Due to Illegal Property Access
Step Two:
The rule of thumb is that only the class itself or its derived classes can access the property.
For example, I will choose just one, namely accessing protected properties in its derived class.
The method:
In the Matakuliah class (derived class), there you create a new method with public access, then define it by returning the $nilai property. And coincidentally it has been created in the previous code example. Pay attention to the part like this!
<?php
...
// Mengambil properti dari class turunan
public function NilaiMhs(){
return $this->nilai;
}
...
?>
Step Three:
In the file "data_mahasiswaA.php", which previously accessed the value property directly, please change it to use a method, by calling the NilaiMhs() method in its derived class.
<?php
...
//echo "Nilai : " .$datanya->nilai; <--- Sebelum
echo "Nilai : " .$datanya->NilaiMhs(); // <-- Sesudah
...
?>
Step Four:
Run the data_mahasiswaA.php file. And the result is Succsessfull!
In essence, protected properties and methods can be accessed in their own class or derived classes.
3. Private Access Rights
Allows a property or method to be accessed only by its own class. Example:
Step One:
Still using the same source code, as above, only, you need to change the access rights for the value property to private.
<?php
class Mahasiswa{
public $nim;
public $nama;
private $nilai; //Rubah hak akses $nilai menjadi private
...
}
?>
When the property is changed to private, then you run the file "data_mahasiswaA.php", then the following error will occur:
Notification Error Due to Illegal Property Access
Step Two:
The rule of thumb is that only its own class can access the property. So what we need to do is create a method in the Mahasiswa class (parent class), there you create a new method with public access, then define it by returning the $nilai property.
<?php
...
public function NilaiMhsPrivate() //method untuk mengakses properti $nilai yang terproteksi
{
return $this->nilai;
}
...
?>
Step Three:
In the file "data_mahasiswaA.php", which previously still used the method in its derived class, now please change it to the method that has been created in the parent class, by calling the NilaiMhsPrivate() method.
<?php
...
//echo "Nilai : " .$datanya->NilaiMhs(); <--- Sebelum
echo "Nilai : " .$datanya->NilaiMhsPrivate(); // <-- Sesudah
...
?>
Step Four:
Run the data_mahasiswaA.php file. And the result is Successfull!, the value has been displayed.
The point is that private properties and methods can only be accessed within their class.
You don't need to worry about the error above, it is because the Grade method in the Matakuliah Class (derived class) also accesses the value property, it needs to be adjusted, the principle is the same as the example I have given.
But, because this is just an example, please ignore it, the important thing is to understand the gist, or just disable the display by adding comment syntax in the data_mahasiswaA.php file.
<?php
...
echo "<br />";
//echo "Grade : " .$datanya->Grade(); <--- Disable Output Grade
echo "<br />";
echo "Status : " .$datanya->StatusNilai();
...
?>
The result;
Thus our review of encapsulation, hopefully it can be useful, greetings young scholars!
Training
Legacy code for learning encapsulation, here is one of the results of my training in the campus lab at that time,
<?php
class kendaraan{
protected $jumlahRoda;
public $warna;
public $bahanBakar;
public $harga;
private $merek;
private function statusHarga(){
if ($this->harga > 5000000) $status = 'Mahal';
else $status = 'Murah';
return $status;
}
function setMerek($x){
$this->merek = $x;
}
function setHarga($y){
$this->harga = $y;
}
function bacaMerek(){
return $this->merek;
}
function bacaHarga(){
return $this->harga;
}
function __construct($x,$y){
$this->merek = $x;
$this->harga = $y;
}
}
class keretaApi extends kendaraan{
public $jumGerbong;
function setGerbong($x){
$this->jumGerbong = $x;
}
function bacaGerbong(){
return $this->jumGerbong;
}
}
//membuat instan
$kereta = new keretaApi("Argolawu", 14000000);
echo "Merek ".$kereta->bacaMerek();
echo "<br>";
echo "Harga ".$kereta->bacaHarga();
$kereta->setGerbong(10);
echo "<br>Jumlah gerbong ".$kereta->bacaGerbong();
?>
Besides that, I also learned about modifiers in PHP, and this is one of the results of my practice at that time.
<?php
/**
* Descripton class RekeningTabungan
* @author 12131314
*/
class RekeningTabungan{
public $saldo;
function __construct($rekening,$nominal){
//$this->saldo = 400000;
$this->norek = $rekening;
$this->saldo = $nominal;
}
public function ceksaldo(){
return $this->saldo;
}
/**
* function transaksi
* Description menampilkan saldo akhir...
* @param int $jumlah_uang
* returnint $saldo
*/
public function transaksi($setor=true,$jumlah_uang){
if($setor){
$this->saldo += $jumlah_uang;
}else{
$this->saldo -= $jumlah_uang;
}
}
public function ambil ($jumlah_uang){
$this->saldo = $this->saldo - $jumlah_uang;
}
}
?>