Sunday, 17 April 2016

Encapsulation

Benefit:
  1. Variable & Method can be well protected. 
  2. Increase the code reusability.

Example:


The name can only modified by the constructor
$student = new Student('John Doe');

The age can only modified by setAge method
$student->setAge(14);

The $age cannot be direct accessed
$student = new Student('John Doe');
$student->age = 14;











No comments:

Post a Comment