What Will I Learn?
- How to learn Encapsulation in PHP
Requirements
- XAMPP
- Editor Text
Difficulty
- Basic
Preliminary
Encapsulation is a method for organizing class structure by hiding the workflow of the class. The class structure is the property and method. With encapsulation, we can restrict access to property and method, so only certain property and method can be accessed from outside the class.
Encapsulation is one of the most important in OOP (Object-Oriented Programming) learning, because it is this stage that determines the granting of permissions on each property and method. Encapsulation in PHP consists of 3 permissions i.e. public, protected, and private.
We can choose which property and method should be accessed, and which ones should not be accessed. By blocking other program codes to change certain properties, the classes become more integrated, and avoid errors when someone 'tries' to change them. Programmers who design classes can provide specific property and method that are intended to be accessed from outside.
Practice Encapsulation
As previously said, encapsulation consists of 3 permissions i.e. public, protected and private. These permissions are placed before the property name or before the method name. Here is the explanation.
- Permissions : Public
When a property or method is declared as public, all the code of the program outside the class can access it, including the derived class. Here is an example of writing public property and public method below :
<?php
// make class tv
class tv {
// make public property
public $owner;
// make public method
public function turnon_tv() {
return "Please turn on the TV";
}
}
// make object from class tv (instantiation)
$tv_simpleawesome = new tv();
// set property
$tv_simpleawesome->owner="simpleawesome,\n";
// show the property
echo $tv_simpleawesome->owner; // simpleawesome,
// show the method
echo $tv_simpleawesome->turnon_tv(); // "Please turn on the TV"
?>
Type the above code in the text editor as below :
Here are the results :
Take a look at the code above, there are property and methods that we set with the public, it can be accessed from outside the class or derived class. As in the example above, we can call method and public property from outside the class.
- Permissions : Protected
If a property or method is declared as protected, it means that the property or method can not be accessed from outside the class, but can be accessed by the class itself or the derived class. If we try to access the protected property or protected method from outside the class, it will generate an error, like the following example :
<?php
// make class tv
class tv {
// make protected property
protected $owner;
// make protected method
protected function turnon_tv() {
return "Please turn on the TV";
}
}
// make object from class tv (instantiation)
$tv_simpleawesome = new tv();
// set protected property will generate an error
$tv_simpleawesome->owner="simpleawesome,\n";
// Fatal error: Cannot access protected property tv::$owner
//
// show protected property will generate an error
echo $tv_simpleawesome->owner;
// Fatal error: Cannot access protected property tv::$owner
//
// run protected method will generate an error
echo $tv_simpleawesome->turnon_tv();
// Fatal error: Call to protected method tv::turnon_tv()
// from context
?>
Type the above code in the text editor as below :
Here are the results (with an error) :
In the above example, calling the $owner
property and the method on turnon_tv()
from outside the class will generate an error.
Although protected level access can not be accessed from outside the class, but can be accessed from within the class itself, here is an example code:
<?php
// make class tv
class tv {
// make protected property
protected $owner="simpleawesome,\n";
public function owner_access() {
return $this->owner;
}
protected function turnon_tv() {
return "Please turn on the TV";
}
public function turnon_force() {
return $this->turnon_tv();
}
}
// make object from class tv (instantiation)
$tv_simpleawesome = new tv();
// run the method owner_access()
echo $tv_simpleawesome->owner_access(); // "simpleawesome,"
// run the method turnon_force()
echo $tv_simpleawesome->turnon_force(); // "Please turn on the TV"
?>
Here are the results :
The property $owner
is declared as protected, so accessing from outside the class will generate an error. Therefore, We created a public method that will display the property $owner
, the owner_acces()
method. Likewise with the turnon_tv()
method that can not be accessed directly. We added a method of turnon_force()
which internally will access the turnon_tv()
.
- Permissions : Private
The last permissions in the encapsulation concept are private. If a property or method is set as private, then the only one who can access is the class itself. Other class can not access them, including derived classes.
For example, here are the code program we can get if we access property and method with private below :
<?php
// make class brand
class brand {
// property with protected permissions
private $brand_name = "Samsung";
public function show_brand() {
return $this->brand_name;
}
}
// make class tv
class tv extends brand{
public function show_brand() {
return $this->brand_name;
}
}
// make object from class tv (instantiation)
$brand_new = new brand();
$tv_new = new tv();
// run the method from class tv
echo $brand_new->show_brand(); // "Samsung"
// run the method from class tv (error)
echo $tv_new->show_brand();
// Notice: Undefined property: tv::$brand_name
?>
Type the above code in the text editor as below :
Here are the results :
In the code above, we make 2 of class, namely class brand and class tv. Class tv is a derivative of the class brand. In the brand class there is a $brand_name
property with private level access. In the class brand and class tv, we create method show_brand()
which is used to access property $brand_name
.
The access method show_brand()
of the $brand_new
object is successfully displayed because it is in a class where the $brand_name
property is located. However, if the show_brand()
method is accessed from the $tv_new
object which is a derivative of the brand class, PHP will issue an error because the $brand_name
property is unknown.
Curriculum
- Tutorial - How to run PHP Programming Language in Command Prompt (CMD)
- Tutorial - Method Chaining in PHP
- Tutorial - Constructor and Destructor in PHP
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit