PHP Tutorial Tutorials - PHP OOP Encapsulation

PHP OOP Encapsulation

Encapsulation is binding the data and behaviors together in a single unit.The data is not accessed directly. It is accessed through the exposed functions(pulic methods). This prevents the client or the user of the module from setting the internal data of the component into an invalid or inconsistent state.

In PHP, Encapsulation is achieved through keeping the object's property private, inside a class.
Other objects don’t have direct access to this property. Instead, they can only call a list of public methods.

<?php
class House {
    private $color = "red"; // declare private property
    public function getColor() {
        return $this -> color; // access the property through public method
    }
}
$house = new House();
echo $house -> getColor(); //red
?>
Date:2019-10-09 00:37:37 From:www.Lautturi.com author:Lautturi