PHP OOP Tutorial: What is Object Oriented Programming and How Does it Work

phpooptutorialFor years, PHP was relegated to the status of a ‘lightweight’ language because it did not support Object Oriented Programming (OOP). It wasn’t until the release of PHP 5 in 2004 that PHP got OOP support and could finally be counted among the ‘serious’ languages.

Even after a decade of OOP support, OOP is still poorly understood among PHP programmers. In this post, we’ll try to clarify the basic theory behind OOP and see how it can be applied to PHP. For a more in-depth examination of OOP, consider this course on PHP for absolute beginners.

Understanding Object Oriented Programming

Take a deep breath – things are about to get really theoretical.

Wikipedia defines Object-oriented programming as a ‘programming paradigm that represents concepts as “objects” that have data fields and associated procedures known as “methods”’.

Unless you speak C, you probably have no idea what that means.

Let’s try again: suppose you want to build a car. This is a very complicated task, so you’ll need a blueprint or a schematic to guide your progress. This schematic will tell you, in precise details, what are the different components used in the car, what order they are assembled in, and the relationship between them. The steering wheel, for example, is connected to the suspension, and the engine to the exhaust pipe. If you had all the required parts, a ton of time, and a slightly mechanical brain, you could use this schematic to build an actual car.

Thus, the schematic is like a class. It includes all the data you need to build something concrete and definite – a car.

The car itself is an object. It is an actual, individual instance of the class. You can use the schematic once to make many cars. The car becomes an instance of the schematic. Similarly, you can declare a class once and use it to make many objects.

Let’s take this a little further. Within the car, there are different components that are themselves made up of several other components. A car engine, for instance, is a very complicated machine by itself, as is the suspension or the transmission. In other words, it can be said that each of these components is an object itself.

By combining different components in different combinations, you can create a huge variety of cars. Similarly, each object exists independently and can be combined with other objects to create complicated programs capable of performing different tasks.

Think of objects as a set of individual components you can turn to when required.

This is the essence of object oriented programming. In conventional, procedural programming, you simply list down a set of commands, and the program runs through down in a linear fashion. In OOP, however, you can create programs by interacting with different classes and objects, all of which are declared separately.

It helps keep your code clean, flexible, and avoids repetition as per the Don’t Repeat Yourself (DRY) principle.

Want to create software but hate coding? Take this course to learn PHP & MySQL for code-phobes.

So, What are the Advantages of OOP Over Non-OOP Programming?

With non-OOP programming, you have to declare everything explicitly and follow a linear process to arrive at a solution. OOP, however, is modular. With OOP, you can combine different ‘modules’ of code to create a solution. It helps keep your code clean and improves flexibility dramatically.

OOP is also useful for modifying existing code. You can change one class or object and see a dramatic difference in the program. With non-OOP programming, however, you have to change the entire code.

OOP also promotes better software design as programmers have to draw plans for different objects to be used in the program in advance. This is particularly true for large applications with several thousand lines of code, which can be nearly impossible to maintain and create using non-OOP procedural programming.

These are just some of the benefits of OOP. You’ll find that the more complex your programs become, the more you will turn to OOP. This is why virtually all large applications are written in OOP languages like C, Java, C++, etc.

OOP in PHP

As mentioned before, PHP was pretty late to the OOP party. It wasn’t until version 5 that PHP could boast of OOP capabilities. Even today, use of OOP principles in PHP programs isn’t particularly common, especially among beginners.

To use OOP in PHP, we will first declare a class.

You can do this with the class keyword.

<?php
     class ExampleClass
           {
           }
?>

Pretty straightforward, right?

Let’s now add some variables to this class:

<?php
     class ExampleClass {
     var $x;
     var $y = “Some Text Here”;
     }
?>

You can add some functions to this as well:

<?php
     class Cars {
           var $x;
           var $y = “Some text”;
           function CarFunction () {
           }
    }
?>

All that follows class Cars comes under class properties and functions. You can think of them as variables, except that they are bound to the specified class can be only accessed by them.

Confused? Maybe you need a PHP punch in the face!

So far so good, but how do we actually define an object?

Simple: with the new command, like this:

$Audi = new Cars;
$Mercedes = new Cars;
$Chevy = new Cars;

We just created three different cars using data from just one class. You can now use the functions included within the class simply by declaring them, one at a time, like this:

$Audi->modelName(“Audi A8”) ;
$Mercedes->modelName(“C Series”);
$Chevy->modelName(“Silverado”);

And so on.

OOP is a vast and complicated topic and we’ve barely touched the surface in this tutorial. But hopefully, some of the basic OOP concepts, specifically, how objects and classes work should be familiar to you now. To learn more about object oriented programming in PHP, check out this killer beginners PHP course.

Speak Your Mind

*

Email