Last Updated:

OOP chips implemented in PHP

OOP (Object-Oriented Programming) is an approach to the development of relatively large projects with a long lifetime. The PHP language is becoming increasingly popular and is used in many professional projects. In view of this, the OOP technique is increasingly used in PHP projects. In this article, we will discuss the OOP technique, which is suitable for use when programming in PHP.

When a project begins, it is not always immediately clear whether it will be large and long or small and rarely modifiable. In such cases, it can be recommended to develop a project using OOP, as this results in a more transparent code that is easier to modify.

It doesn't make much sense to use OOP for small projects that will exist for a short period of time. However, if you plan to add new features in the future, etc., then OOP will save you time.

Another important note about OOP (which can be attributed to many aspects of programming) is that no one can properly use OOP until they have gained enough experience. University courses and self-education are not enough. People who study OOP get good grades, have a degree, but often take a procedural approach to programming. The only way to use OOP correctly is to experience and learn from your own attempts and mistakes. Sometimes beginners think that if they use classes, then they use OOP. This is completely incorrect. You can use the classes but continue to program in a procedural style.

The following is a list of OOP concepts and is discussed from the implementation in PHP.

  • There is no multiple inheritance in PHP and it is highly unlikely that it will appear at all. Many OOP languages do just fine without it (Java, ObjectPascal, C#)

  • Overloading methods works fine. You can rewrite the parent class method in the successor class.

    class A { function foo() { echo "A::foo();"; } }
    class B extends A { function foo() { echo "B::foo();"; } }
    $obj = new B();

    returns B::foo();

  • Calling basic functions works exactly as in the C++

    class A { function foo() { echo "test A"; } }
    class B extends A { function foo() { A::foo(); echo " from class B"; } }
    $obj = new B();


    Returns test A from class B

  • Virtual functions. By definition, all class methods are virtual. Each object "knows" its class and all its functions.

    class A
      function foo() { echo "1"; }
      function boo() { $this->foo(); }

    class B extends A
      function foo() { echo "2"; }

    $b = new B();

    Returns 2, which demonstrates that the foo() function is virtual.

  • Polymorphism. Because all methods are virtual and variables are not declared, polymorphism is present by definition.

  • Static functions. If a class method does not reference any property (variable) of the class, it can be called static.
    For example:

    class A { function foo() { return "test"; } }
    echo A::foo();

    Returns test.

  • There are no static class variables. In general, it is a controversial question whether PHP needs static variables. Some OOP principles say that static variables are not needed. However, there are exceptions when they are useful, such as $HTTP_POST_DATA and the like. It is possible to make a global static variable outside the class and access it through $GLOBALS or declare it global explicitly when accessed.

  • There are no templates (parameterized classes), but they are not needed because variables are not declared and there are no strong types. For example, you can store objects that belong to different classes (as well as numbers and strings) in the same array.

  • No private/protected variables and functions. Actually, there would be nothing wrong with having them, but they are very rarely needed in languages like PHP.