DEV Community

Mohamed Fakhr
Mohamed Fakhr

Posted on

OOP Object-Oriented Programming

Advantages of using OOP:

  • Is faster and easier to execute.
  • Provides a clear structure for the programs.
  • Helps to keep code DRY "Don't Repeat Yourself" and makes code easier to maintain, modify, and debug.
  • Makes it possible to create fully reusable applications with less code and shorter development time.

Define a Class:
A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces {}. All its properties and methods go inside the braces.

Delegation:
Delegation means that you use an object of another class as an instance variable.
We can create multiple objects from a class. Each object has all the variables and functions defined in the class. An object of a class is made using the new keyword.

Note:
The $this keyword refers to the current class and is only available inside methods.

__construct() function:
Automatically runs at the beginning of the class.

__destruct() function:
Automatically runs at the end of the class.

Encapsulation:
The wrapping up of data and methods is a protection mechanism for the variables and functions inside the class.

Access Modifier:
Public: Variables or functions can be accessed from everywhere.

Private: Variables or functions can ONLY be accessed inside the class.

Protected: Variables or functions can be accessed inside the class and by child classes that extend from the parent class.

Constants:

  • It can’t be changed once it is declared.
  • Declared inside a class with the const keyword.
  • It is recommended to name the constants in all uppercase letters.
  • Access outside the class by using the class name followed by the scope resolution operator ::.
  • Access a constant inside the class by using the self keyword.

Static Functions and Variables:

  • Static functions or variables can be called directly - without creating an instance of the class first.
  • Static functions or variables are declared with the static keyword.
  • To access a static function or variable, use the class name, double colon ::, and the function name.
  • A static function can be accessed from a function in the same class using the self keyword and double colon ::.
  • To call a static method from a child class, use the parent keyword inside the child class.
  • The static method can be public or protected.

Note:
The final keyword can be used to prevent class inheritance or to prevent method overriding.

Polymorphism:
is one of the OOP features that allows us to perform a single action in different ways.
Like declaring a function in the parent class and giving the function a different implementation in the children's classes.

Abstract:

  • Can have both abstract methods (without body) and regular methods (with implementation).
  • Can have properties (variables).
  • A class can extended single abstract (single inheritance).
  • Cannot be instantiated directly β€” only extended (inherited) by other classes.
  • Used when classes share some common behavior, but also need to implement some parts individually.

Interface:

  • Can only contain method declarations (no implementation).
  • No properties β€” only method signatures.
  • A class can implement multiple interfaces (multiple inheritance).
  • Forces the class to implement all methods defined in the interface.

Traits
PHP only supports single inheritance: a child class can inherit from only one parent.
So, what if a class needs to inherit multiple behaviors?
OOP traits solve this problem.

  • Traits are used to declare functions that can be used in multiple classes. Traits can have functions and abstract functions that can be used in multiple classes, and the functions can have any access modifier (public, private, or protected).
  • Traits are declared with the trait keyword.
  • To use a trait in a class, use the use keyword.

Difference between Traits and Interfaces in PHP
many classes implement the same interface but have different behavior, while traits are just chunks of code injected into a class in PHP.

Top comments (0)