Skip to content

Programming Pills – Encapsulation

Encapsulation is used to protect the attributes of a class and make them manipulable only by interfaces made available to communicate with the outside world. Typically they are getters and setters. This protects attributes and ensures that only valid values are assigned to them.

Attributes and methods can have three kinds of “visibility”:

  • private: means that the attribute or method is only visible from that class
  • protected: attributes/methods are available for the class itself and the classes derived from it
  • public: everyone has access to attributes/methods

As already mentioned, attributes should be editable and visible only through interfaces made available by the class to communicate with the outside world. Typically they are getters and setters.

Getter

The getter functions are used to obtain an attribute.

public function getName() {
    return $this->name;
}

Setter

While the setter functions are used to set the attributes from the outside.

public function setName(string $name) {
    $this->name = $name;
}

Compared to procedural programming, in object-oriented programming, encapsulation allows you to reuse a class (and its methods / attributes) several times as if it were, in fact, a capsule.


Photo by Markus Spiske on Unsplash

Published inIT BasicsTech Blog

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *