Encapsulation is wrapping some data in an object. It is about controlling the visibility of things and define something like the order of layers of our application.
Encapsulation in PHP OOP is about control and limit which parts of our application can access some class methods and properties using three keywords called in OOP world Access Control Modifiers , and these keywords are : public , private , protected and this keyword comes in the beginning of declaring method or property just like below :
class Member {
public $id;
protected $username;
private $password;
}
Public keyword modifier is used to define a variable or function as being visible to everyone, everywhere. When this modifier is used the variable or function following is visible to the users using your class as well as all the inner workings of your class. This modifier is the only modifier that allows interaction outside of the class.
Private keyword modifier is used to define a variable or function as being visible only to the inside of the class. When this modifier is used the variable or function following is invisible to the users using your class, but all the code that you write inside your class will be able to see it.
Protected keyword modifier is similar to the Private keyword modifier in that it is visible inside of the class but not to users outside the class. In addition to that however, the protected keyword also allows child classes that extend the class to see the characteristics as well.
Encapsulation in PHP OOP is about control and limit which parts of our application can access some class methods and properties using three keywords called in OOP world Access Control Modifiers , and these keywords are : public , private , protected and this keyword comes in the beginning of declaring method or property just like below :
class Member {
public $id;
protected $username;
private $password;
}
Public keyword modifier is used to define a variable or function as being visible to everyone, everywhere. When this modifier is used the variable or function following is visible to the users using your class as well as all the inner workings of your class. This modifier is the only modifier that allows interaction outside of the class.
Private keyword modifier is used to define a variable or function as being visible only to the inside of the class. When this modifier is used the variable or function following is invisible to the users using your class, but all the code that you write inside your class will be able to see it.
Protected keyword modifier is similar to the Private keyword modifier in that it is visible inside of the class but not to users outside the class. In addition to that however, the protected keyword also allows child classes that extend the class to see the characteristics as well.
Comments
Post a Comment