OOP in PHP for mid-level developers

februarie 20, 2024 by

Andy

These are my own documentation to help me get more into the OOP coding style, from the position of using functional PHP professionally for quite a while. So these are just some notes, but I thought to make them public in case they might be useful to anyone else:

The four pillars for OOP are Abstraction, Encapsulation, Inheritance, Polymorphism.

  1. Abstraction: Abstraction is the process of showing only essential/necessary features of an entity/object to the outside world and hiding other irrelevant information. For example, to open your TV we only have a power button, It is not required to understand how infra-red waves are getting generated in TV remote control.
  2. Encapsulation: Encapsulation means wrapping up data and member function (Method) together into a single unit i.e. class. Encapsulation automatically achieve the concept of data hiding providing security to data by making the variable as private and exposing the property to access the private data which would be public.
  3. Inheritance: The ability to create a new class from an existing class. Inheritance is when an object acquires the property of another object. Inheritance allows a class (subclass) to acquire the properties and behaviour of another class (super-class). It helps to reuse, customize and enhance the existing code. So it helps to write code accurately and reduce the development time.
  4. Polymorphism: Polymorphism is derived from 2 Greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means “many forms”. A subclass can define its own unique behaviour and still share the same functionalities or behavior of its parent/base class. A subclass can have their own behavior and share some of its behavior from its parent class not the other way around. A parent class cannot have the behavior of its subclass.

ro_RORomână