Object-oriented programming with PHP (Part 1)

Tram Ho


Object-oriented programming ( OOP ) is a programming technology that supports object technology. If it used to be procedural, module-oriented, and so on, now the world is favored with object-oriented programming. to handle, now when using object orientation, we will split it into objects for processing.

The following article will focus on addressing the following issues:

  • The basic characteristics of object-oriented programming How are they expressed in PHP.
  • Differences between Abstract Class and Interface.
  • What is a static function? Distinguish using the keyword static :: method () with self :: method ().
  • What is Trait.
  • What is Namespaces.

1. The basic characteristics of object-oriented programming How are they expressed in PHP.

Object-oriented OOP ( OOP ) has 4 basic characteristics:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation : Encapsulation is expressed by the fact that the properties and methods are hidden in a class , with the purpose of controlling usage rights and accessing properties and methods.

The encapsulation in PHP is expressed through the use of the keywords public , private , protected :

  • public : Lets use the properties and methods of the class inside, outside the class , the class inherited from there can be flexibly reused.
  • private : Only allowed within the class .
  • protected : Allows use in scope in parent class and class inherited from parent class .

Inheritance : expressed by class inheriting methods and properties of the parent class .

Abstraction : in object- oriented programming helps reduce complexity by focusing on key characteristics rather than going into details. Thus when interacting with objects only need to care about the properties and methods needed. Details of the content need not pay attention to.

PHP has abstract class and interface to abstract objects.

Polymorphism : expressed by the class can rewrite methods from the parent class , through extends and implements

2. Differences between Abstract Class and Interface.

In part 1, we mentioned two concepts of abstract class and interface . Interface and Abstract class are 2 basic concepts in OOP programming. But most people feel confused and confused by these two concepts.

Abstract class : is an abstract class for all classes with the same nature, therefore, each derived class (subclass) can only inherit from an abstract class. Besides, it does not allow instance creation, meaning it will not be possible to create objects of that class.

Interface : This class is considered as a mask for all classes with the same operation but may differ in nature. Since then, the derived class can inherit from many Interface classes to fully complement the way its operation ( Multiple inheritance ).

3. What is a static function? Distinguish using the keyword static :: method () with self :: method ().

What is a static function? : The static function is a function that can be called without needing an object of that class . Static it acts as a global variable even if it is processed in any file (in the same program), it will save the final value that it is made into the class.

The implementation of the static function in the class can be done by the command: static::staticMethod() , self::staticMethod() or $this->staticMethod() , where self and static represent the class , and $this is the representation of the object . In static methods, non-static methods or properties cannot be called. But the methods non-static can call the method or attribute static . Because it can be understood as follows:

  • The static method can be called even before the object has been initialized, so if the static method calls on a non-static , when the object not been initialized, there will be no $this variable (representing the object ) to call. to non-static methods.
  • Naturally, the non-static can always call the static method because the static method already exists from the beginning of the program, when the object not been instantiated.

Distinguish using keyword static::method() with self::method() :

  • static : represents the object itself calling the real method
  • self : represents the object that declared it.

4. What is Trait.

Traits simply mean a group of methods that you want to include in another class. A Trait similar to an abstract class cannot be instantiated on itself. Trait reduces the limitations of đơn kế thừa , allowing us to reuse a group of methods on the class .

A simple Trait could be:

We can use it in different class as follows:

Thus, when you initialize objects from the Post and Comment classes, that object will have the share() method available.

We can use multiple Trait in a class .

5. What are Namespaces.

Namespace helps create a namespace for functions and classes in programming in general and in PHP in particular. With PHP, Namespaces are designed to solve the two problems of authoring libraries and applications when reusing classes and functions.

  • Namespaces avoid function names and class names that may overlap when you create them with PHP or third-party functions, classes, variables.
  • Namespaces have the ability to create aliases, shorten naming practices, and make code more readable.

PHP Namespaces provides a way to group related class , interface , function , and constant .


Through the article, I hope you have understood the basics of object-oriented and its representation in PHP, some widely used concepts.

Share the news now

Source : Viblo