PHP Object Oriented Programming (OOP)

OOP stands for (Object Oriented Programming) which is the complicated & advanced form of web development in PHP/MySQLi. There are two different ways to write a program in PHP, the one is called “Procedural Method” in which you simply write the codes directly in the text editor wherever you want and get the results in the browser, and the second programming method is the “OOP” one which is basically object based. That’s mean, you have to create objects for each task you want to perform in PHP. On this blog, you’ll find many PHP tutorials with procedural method, and there are projects too such as Complete CMS in PHP and eCommerce Website in PHP. However, in today’s tutorial, I’ll give you an insight over object oriented programming in PHP, this is basically an introduction to this advanced method of web programming, but in near future, I’ll create a video course on it, so you can then build projects as well.


What’s Object Oriented Programming?

As I mentioned above, it’s a complicated way of coding in PHP, and similar to that, it’s also referred that OOP is the advanced style of writing PHP codes. However, OOP concept is existing almost in every programming language i.e C++, C#, Java and many other. The concept of using OOP in Web languages was already in JavaScript & But in PHP it just came into being after version # 5.0 in 2005. It’s a secure way to program in any web language. An object is basically something on which you want to write a program, so you first define the object and then add its methods & properties which you can access anytime and anywhere (but with limits).

How to Do OOP in PHP?

Now after we know what’s OOP, let’s create a very small & simple program so that you can understand it more clearly, first I’ll write a code using procedural method in PHP and after that we’ll convert that code into OOP, how’s that? cool, let’s write some very basic code in PHP first and then convert it into OOP style.

If I want to write a simple PHP code which displays the name & age of a person, that would be something like below:


//The Normal Method to Program in PHP

$name = “Roben Hod”;

$age = 35;

echo $name . ” is “.$age . ” Years Old.”;


This simple program will display the following result on the page:

Roben Hod is 35 Years Old.

Now if we want to convert this code into OOP style then simply we have to create a new object which is obviously a person, so after creating the main object, we then can add methods & properties to it. And anytime we can access & display them on the page. Have a look at this code:

// The OOP Method to Program in PHP$object = new person;

class person{

public $name = “Roben Hod”;

public $age = 35;


echo $object ->name . ” is ” . $object->age . ” Years Old”;


Using the above code, if we go to the page, we’ll see the same result:

Roben Hod is 35 Years Old. 

But we did the above one using OOP method, However, you might be thinking about the code I’ve written in the OOP style, but don’t worry, I’ll explain it to you.

While coding in OOP style, you have to first define an object with any name for example we defined (person) in above example, you can define an object by using (new) keyword, and save the new object into a local variable as we did like ($object = new person), after you created the object the next step is to create a class for it, the class basically holds all the associated data related to that object we created, give the same name to the class and two squarely braces i.e class{}. Now, in the class you can add properties & methods, for example I added ($name & $age) properties and I assigned values as well, now the next part is to access the properties/methods & display on the page, for which you need to first select the object & then access the property you want using this syntax ($object->name).

You can add function to the class using this method: 


$object = new person;

class person {

public function body(){

echo “Head, Hairs, Face, Hands!”;





In the above example, you’ll see a result on the page like this:

Head, Hairs, Face, Hands! 

This is because we created a function which is called method in OOP, and we echoed some values inside the function and later on we accessed the function within the object.

Now, you can watch the video tutorial below to understand what we did in the article, so you’ll be able to create your first small script using object oriented programming in PHP.

If you got any questions then let me know in the comment section of this post, and take a few seconds to share this tutorial with all of your friends, happy web development!.

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also

web hosting

The Best Web Hosting Providers (Tutorial)

Web hosting is one of the important aspects for establishing a business or website online. If ...

  • Very nice example, The nature of Object Oriented Programming is like messing up with arrays. Great article for newbies to understand OOP.
    Keep blogging on nice things 😉

  • tenzin

    Hello sir,please upload more video and project on oops as soon as possible…..

    • Hi Tenzin,

      I’ll do it soon, keep in touch!

  • tenzin

    create more video on ooops project

  • Sayyed Aziz Ahmed Qadri

    Assalamualaikum wali bhai aap ka tutorials bahut accha hai kya aap koi college website project banaye ho mera topic college website hai,by the way iam in tybscit.-Abdul Aziz

    • w/salam,
      I have not created any college website project yet, you can find all the projects under the “Web Development” or “Projects” category on my website.

  • Sayyed Aziz Ahmed Qadri

    Ok , i request you please make one it will help me!