Encapsulation
The meaning of Encapsulation is to make sure that “sensitive” data is hidden from users. To achieve this, you must declare class variables/attributes as private
(cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.
Inheritance
Inherit attributes and methods from one class to another.
We group the “inheritance concept” into two categories:
- derived class (child) – the class that inherits from another class
- base class (parent) – the class being inherited from
It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
Access Specifiers
public: members of a class are accessible from outside the class
private: members can only be accessed within the class
protected: similar to private, but it can also be accessed in the inherited class
Polymorphism
(Overriding methods)
Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance. Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
For example, think of a base class called Animal
 that has a method called animalSound()
. Derived classes of Animals could be Pigs, Cats, Dogs, and Birds – And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):
Why And When To Use “Inheritance” and “Polymorphism”?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
PHPÂ OOP – Abstract Classes
Abstract classes and methods are when the parent class has a named method, but needs its child class(es) to fill out the tasks.
An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared but not implemented in the code.
So, when a child class is inherited from an abstract class, we have the following rules:
- The child class method must be defined with the same name and it redeclares the parent abstract method
- The child class method must be defined with the same or a less restricted access modifier
- The number of required arguments must be the same. However, the child class may have optional arguments in addition
PHP – What are Interfaces?
Interfaces allow you to specify what methods a class should implement.
To implement an interface, a class must use the implements
 keyword.
A class that implements an interface must implement all of the interface’s methods.
PHP – Interfaces vs. Abstract Classes
Interfaces are similar to abstract classes. The difference between interfaces and abstract classes are:
- Interfaces cannot have properties, while abstract classes can
- All interface methods must be public, while abstract class methods is public or protected
- All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary
- Classes can implement an interface while inheriting from another class at the same time
PHP – What are Traits?
PHP only supports single inheritance: a child class can inherit only from one single parent.
So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.
Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
PHP – Static Methods
Static methods can be called directly – without creating an instance of the class first.
To call a static method from a child class, use the parent
keyword inside the child class. Here, the static method can be public
 or protected
PHP Namespaces
Namespaces are qualifiers that solve two different problems:
- They allow for the better organization by grouping classes that work together to perform a task
- They allow the same name to be used for more than one class
PHP Enum
An enumeration, or enum for short, is a new feature to declare a custom type with an explicit set of possible values.