
Starting out with object orient programming or OOP for short can be a daunting concept. In this post, we will look at OOP in PHP but the concept can be shared across other programming languages such as Java, Python, Ruby, C++ and more.
A quick look at OOP can make it appear more complex than your standard procedural code. That doesn’t have to the case. OOP allows you to group similar tasks into containers. In PHP a container is called a class. A class contains all the functions and variables that define an object.
These functions are called methods & variables are called properties. Object-oriented programming can be compared to how we look at things in the real world this can help visualise a solution to a problem more easily. Let’s take a real-world example.
A house is defined by a blueprint which shows the structure, rooms and the exact design for a house. Each house has attributes like walls, a roof, windows, and doors. They also have actions such as opening a door or a window. A blueprint itself is not a house but, it describes how a house should be built. Opening a door or a window are actions and the roof, windows and doors etc are properties.
Each time a new house is built we create a new object in this example a house object. If the blueprint is a class named house we can build as many houses as we please. This is called instantiating an object or creating an instance of an object. Each house object shares properties and methods but individual values of a house may be different for example the color of a door or walls.
By grouping our properties and methods into a class we can make it easier to not to repeat yourself and follow D-R-Y (don’t repeat yourself), making it easier to maintain your application while maximizing reusability. D-R-Y is one key principle to follow in using OOP the other is K-I-S-S. Keep-it-simple-stupid, This means don’t use fancy features just because you can. Keep code simple code as it’s less prone to bugs and easier to read and understand.
Why Use OOP?
In the role of a developer, you will come across OOP sooner rather than later. The better you understand the more effective you will be as a developer. One of the main benefits of object-oriented programming is its modularity. By keeping different parts of the code base separate you are able to change one piece of the application without affecting others. This becomes an even greater benefit when working in a larger project with more team members as it allows one developer or developers to be in charge of one section of the code without knowing the other aspects of the project. This also increases the reusability of the code, as a well-designed object should only have a specific function therefore completely independent of the environment it is placed in. Thus, it can be replaced and reused easily.
This modularity also allows large applications to be more easily maintained. These separate pieces can be tested independently, so fewer bugs make it into production.
OOP Frameworks
Because of these benefits, modern PHP frameworks such as Laravel, CodeIgniter, CakePHP, and Zend are written using object-oriented methodology. Using a framework is like having a large portion of a puzzle done for you so all you need to do is put those pieces together. This helps launch a project more quickly than developing everything from scratch.
Summary
The fact that most modern PHP projects are written using object-oriented programming. You should not be afraid of adding OOP to your skill set. You’ll be able to work directly on many more projects, contribute to open source software, translate your knowledge into other object-oriented language and in turn learn more as a developer. This is just the start of our conversation next we will look at the basics of using OOP and how to use it in your own project. But that’s another blog post for another time.