PHP7 OOPs Encapsulation

Encapsulation is a key of object oriented programming concept. It simply keep secure the data and functionality from the direct access of client. It is actually a process of wrapping up the data and methods into a single entity called class. Data is not accessible outside and only those methods, which are wrapped in can access it.

Example

<?php
    class Company {
        // Properties
		public $name = 'Smith';
        protected $title = 'ETP'; 

        // Method
        function welcome() {
            echo 'Welcome to the '.$title.' Company.';
        }
    }
    $obj = new Company();
    echo $obj->name;
    echo '<br/>';
    echo $obj->welcome();
?>

Output

Smith
Welcome to the Company.

As you can show from the above example, the properties and method are wrapped in a single entity.

The access specifiers Private, Protected and Public have made encapsulation easier.







Read more articles


General Knowledge



Learn Popular Language