Introduction to Object Oriented Programming in PHP


Object Oriented Programming is the approach to group all the variables and functions of a particular functionality/topic in a single class. As compared to procedural programming style, Object Oriented Programming is considered to be fast, advanced, and efficient. We may prefer procedural programming style in simple and small projects but as the complexity of the project grow, we should better off using object oriented programming approach for better code efficiency, re-usability, and security.

Example 1:
This universe is made up different objects like Planets (Earth, Moon, Venus, Jupiter etc), galaxies, asteroids etc. So in this case of universe, the concept of object oriented approach would be:
Class: Universe
Objects: Planets, Galaxies, Asteroids etc.

Example 2:
Let us have the most common example to understand Object Oriented Programming Approach. Just think about the Cars which we use regularly, car has seats, engine, gears, wheels, body etc. Keeping these all things in our mind the OOP concept would be visualised in the following way:
Class: Car
Objects: Seats, Engine, Gears, Wheels, Body etc.

In the same way Object Oriented Programming approach assumes everything as an object and develop a software/application using various different objects.

Object Oriented Programming in PHP
In year 2000, Object Oriented Programming was first introduced in PHP4 but at that time the implementation of OOP was not very vast in PHP4. In 2006 with the release of PHP5 with complete OOP approach, PHP developers finally had the power to code like C# and Java like languages. Instead of PHP6, Zend company launched the PHP7 with the advancements in object model structure with addition of more features like Interfaces, Triats etc.

There are some fundamental concepts of Object Oriented Programming – OOP on which this totally based on. Abstraction, Encapsulation, Inheritance, Polymorphism, Interfaces, Constructor, Destructor, PHP access modifiers, and Abstract Classes are some of the most important concepts of OOP.

Problems/Issues with Procedural Programming 
Functional Programming is fine for small or medium projects but for large projects it was very difficult to follow this procedural programming approach. Below mentioned are some of the problems with procedural/functional programming:

  • Non reusable functions
  • Unavailability of proper naming conventions while creating functions
  • We cannot modify or extend the functionality of functions
  • No way to hide or encapsulate the function names

Advantages of OOP in PHP
We can create a package of our code using classes and because of this change classes would not affect any other part of the application/program. So here are some advantages of using OOP.

  • Very easy to manage & maintain large projects using a dedicated structures of classes & objects.
  • Security implementation using encapsulation which helps the programmers to hide the code from outside world.
  • Inheritance allows the programmers ot extend any part of the code without affecting any other part.
  • More features and better performance with rewritten object model.
  • Addition of new features like magic methods, abstract classes, interfaces, access modifiers, cloning and type-hinting.

In our next tutorial, we will explain about Classes & Objects, Access Modifiers, Inheritance, Abstraction, Encapsulation, Polymorphism etc with real time examples, so that you all will be able to understand the concept of OOP easily.

You may also like...

Leave a Reply