What is Object Oriented Programming in PHP?

Object oriented program is an approach to imagine everything in the form of objects like wheel, steering, gear etc. Similarly, the approach of objects applied to our software programming.  It is a programming based on objects containing data (attributes)  and methods (functionality) which work on that data. Objects are data type defined by programmer and interaction between different objects is also defined by programmer.

In other words, object oriented programming allows programmers to group similar tasks in to classes, a class may have data and the functions to access that data, tied together not visible from outside.

The major advantage of using Object Oriented approach is the more re-usability and ease in maintenance.  It doesn’t matter that you are familiar with the OOP before or not, I am going to explain this in a very much simpler manner that how and why to use it.

OOP basically focuses on designing and programming individual classes and how objects behave and granting access to the datatypes.

Note:  OOP is not difficult to learn, but the difficult part is, how you can program well in it, because most of the programmers, new to OOP, come from a procedural background.

In php 5 a major change from php 4 is that it contains a full object model for more features and better performance. Some of the important new features added in php 5 are final and abstract methods and classes, interfaces, cloning, and magic methods. We have covered all these in our php oop tutorial series with practical exammples. In object oriented programming, an object is treated just like references or handles, which means every new variable has a reference of the object and not a copy of entire object.

When we talk about object oriented php, the most difficult thing to learn and understand are the basics of object oriented programmnig, as they are different from usual php programming however, once the basics are clear the model becomes easy and enjoyable. This is the easiest, simple and comprehensive php oop tutorial on web, explaining everything in detail from classes to objects, constructors to destructors, inheritance and interfaces, access modifiers and so on. This is one place to grasp php oop concepts once and for all in a simple way. It is a common observation that oop in php is a dilemma for php programmers as it is new style for them. Here we will provide oops concepts in php which you can comprehend easily.

Concepts of Object Oriented Programming :

Class : A class is a template which contains collection of members like variables, functions and constructors etc. A class has data and functions to operate on that data. This data and functions are usually private which means they are not visible outside the class for data security. A class is a template or blueprint to make as many copies or instances as required.

You can invoke the members of the class by creating an instance or object reference to the class.

Object : An object is nothing but an instance of a class. You can define many objects to class. When a class is instantiated an object is created. If a class is blueprint then object is the final product of that blue print. Once a class is defined then as many objects can be created from that class as needed.

Member Variable : A member variable is defined inside of a class. The data which is there in the member variables are limited to the class and can by accessed by the members (functions) of a class. Actually it is the data contained in this class and can be modified by functions of this class only. Usually member variables are only visible to that class and are hidden from other classes.

Member Function : A member function contains collection of statements. It is defined inside the class and used to access object data simply. These are usually private, not visible to other classes. They are used to modify member variables. In other words they are helpful in accessing data of an object.

Inheritance : The process of creating a new class based on existing class is nothing but inheritance. The new class is called child class and the existing class is called parent class.

In oop php inheritance is a process in which a class (sub class) gets all the attributes and functions of another class (super class).

Polymorphism : This concepts tells us to same functions in different forms. The name of the function can be the same but it may contain different formation or different number of arguments.

It is a phenomenon in object oriented programming in which same function can be used for different purposes.

Overloading : Its a part of Polymorphism which allows us to create same methods by differentiating number of parameters in the functions.

Data Abstraction : The data in the class which is hidden or abstracted.

Encapsulation : The process of binding data members and member functions in the class is called Encapsulation. Data and the functions that work on that data are tied together and not visible to other functions.

Constructor : It is a function called automatically when we create an object reference or instance to the class. It is used to initialize class members.

Constructor is actually a special type of member function. When a class is instantiated, an object is created this function is called automatically and it assigns some initial values to data members.

Destructor : It is a special type of function which is called automatically when an object is deleted.

Abstraction : Abstraction means that the implementation details of functions or classes are not visible.

Overloading : An oop term in which Functions having same names but different number of arguments, performing differently.

Some major advantages of OOP and how it is more efficient than procedural approach.

PHP OOP Benefits:

Reusability of code:

In OOP, code is easy to organize, maintain and can be reused with a great ease which leads to much neater code. Separate components or modules could also be programmed in OOP, which can perform a specific task. OOP is perfectly suitable for code reusability and eliminates the chances of code duplication as much as possible. The major purpose of developing this approach was to reduce unnecessary or unused coding which definitely reduces project programming time and hence low budget is required. Objects have eliminated the concept of cut, paste, and adapt existing code. Functional programming is also a good approach in terms of usability. Please have a look at Interface Design and Composition and see how OOP components could be reused. {loadposition chitika}

Suitable for designing distributed systems:

The OOP UML paradigm is useful in designing distributed systems or framework with a lot of entity living during the actions of the user (example: web application). The object oriented approach is more modular in nature and thus more suitable for distributed systems. It is also ideal for instances where too many developers are involved in the programming of an application. Some people believe that in distributed systems OOP takes more development time than structured approach but it has a lot of advantages in the long run and this delay is only once in the application programming.

Automatic use of memory:

OOP is good because it uses garbage collection during the execution of a program.  While in a structured approach, a problem in memory is manually debugged and corrected. You can see manual memory allocation in languages without automatic memory management, like C language, which uses different built-in functions to do so, when they are called in the code. It depends on programmer that he can reserve memory according to requirement and can also set it free, when he determines that this will not have any bad impact on the running of program or software. Not all OOP languages provide garbage collection.

Best in modification and error handling:

It is very easy to modify or adding more features to a class than a structured code, because OOP code is in an organized form. Similarly error reporting capability of OOP code is also very good. Suppose you are working on a project and on a specific phase the requirements of the client has changed. Then if you are using OOP, modification would be very easy.

As there are well defined objects, with data and functions usually as private. There are clear communication pathways and no wandering data or method so smaller amount of bugs exist in oop. Hence Object oriented programming is the ultimate solution in new systems. And php oop tutorial is a free comprehensive tutorial for you to learn all tricks of the trade. So get started by clicking Next for studying php class. Wish you good luck for your Object oriented programming in php journey 🙂

Better Architecture, Neat and Clean code:

Object oriented php is better cataloged and easily packaged creating a wonderful architecture. Usually each class is saved in a separate file. Procedural languages have a complex spaghetti like code, very difficult to spot or understand the functionality of a certain characteristic or data member. In oop due to implementation of objects, having their private data and members and a clear understanding of communication between objects the code is nice and clean.

The major objective of OOP is to write cohesive, neat and clean code. It is ideal for developing huge libraries and frameworks, as it has made maintaining and customizing existing code is so much easy. Data Encapsulation and inheritance aspects of OOP also make it ideal for huge projects, make the code cleaner and easy to debug.

Easy to maintain and upgrade.

Objects have their private data and functions, whole structure is compact and concise which makes it easy to make changes in code, usual procedural programming has spaghetti type of code making it almost impossible to spot the point to modify. A new property can be added and then related methods can be added to control that property.

Please note that there are a huge number of programmers which find OOP, not the best solution for developing a software.

Arguments from the other side.

1 – You can write great code in non-OOP languages as compared to writing terrible code in OOP. You can do that in just about any paradigm, not just in this approach. In some situations OOP code has so many tight dependencies between objects that it becomes impossible to pull out a single piece for reuse.

2- You have to write more code in OOP, as compared to normal structured approach. Which definitely means that the software or program would be slower. For a web application, delay in load time also increases the bounce rate.

3 – if not properly designed, coded and maintained, it is very difficult to troubleshoot as compared to a structured procedural language code.

4-  Modification and clean code have also nothing to do with OOP.

There is a detailed debate regarding the advantages and disadvantages of OOP. Some people find it useful and some do not. There are a lot of circumstances in which OOP is the best solution and there are lot of situations also, where structured approach is best. So, its up to you that how you take a decision, by identifying the problem first.


As I said earlier, classes have been proven to be very efficient. They are the gateway to “Object Oriented Programming” or OOP as some people like to call it. With OOP, you can dramatically save yourself a lot of time and also create an archive of objects or classes that you can use over and over.

Add a Comment

Your email address will not be published. Required fields are marked *


My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote