Join Kevin Skoglund for an in-depth discussion in this video What is OOP?, part of PHP: Object-Oriented Programming.
- [Instructor] Let's begin by talking about what is OOP. OOP is an abbreviation for object-oriented programming. Object-oriented programming is a type of programming where we define data structures, those are the objects, which can hold both values and functions. PHP is a scripting language or a procedural language. If you've been using PHP already, then you've been setting variables and you've been defining functions. Working with PHP in this way is considered procedural, not object-oriented.
Our code changes are rather linear as we move down the page of code. We set values, we call functions, we work with the results. We're calling procedures as we move down the page. PHP is not considered an object-oriented language. Instead, it's a scripting language with object-oriented features built into it. Features that were added in PHP five. Now, the fact that PHP isn't fundamentally object-oriented isn't a problem, it just means that we have a choice. We can either work with PHP in a procedural way or in an object-oriented way.
So, how do you know if you need object-oriented programming? Well, if scripting and procedures are all you want PHP to do, then you may not benefit from it. But, if you're asking PHP to do more complex work, then OOP may be exactly what you need to manage that complexity. Here's my rule of thumb. For a simple site, object-oriented programming adds unnecessary complexity, but for a complex site, object-oriented programming adds necessary simplicity. And database-driven sites generally have enough complexity that they will benefit from object-oriented programming.
So, what exactly are these objects that we're talking about? Well, in the simplist sense, objects are simply grouping code together by a common theme. So, if you think back to some of the work we've done in the past where we were working with forms that people are submitting, we write functions that relate to those forms. We could take all of those functions and roll them up into a form object and then our code would be grouped together by a common theme and we'd be able to find those functions all in one place. We can also use software objects to create abstractions for concepts in our code as if they were real-world objects.
And this is probably a more common use than just grouping them together by some basic theme. If we have a website that's selling products, then we would probably have a customer object and that would be an abstraction for the real-world customer object. We would probably also have a product object that we're selling the customer; a shopping cart object; a credit card object; and so on. Objects don't have to have real-world analogs either. If we have a blog, then we might have a blog post object and if users are able to comment on the blog post, then we would probably have a user object and a comment object as well.
Remember, object-oriented programming is about creating data structures which hold values and functions. We call those properties and methods. These are the fundamental elements for defining and working with objects. Properties are just variables inside the object. They define the characteristics of a particular object. When we think of real-world objects, we naturally think of their properties. The dress is green; green is a property of the dress. The table is four feet long and two feet wide.
The house has three bedrooms and two bathrooms. In code, object properties might be a student object which has a first name and a last name; or a book object which has a title, an author, a year, and the number of pages. These are all just variables that we set to define the properties of a particular object. Methods are just like our regular procedural functions, but they're stored inside the object so that the object knows how to do something. For example, a shopping cart object could have a method called total, which would inspect the properties of all the product objects in the shopping cart, get their prices, and return back the total amount.
This is where objects get a lot of their power because we can add methods to teach them how to perform tasks, to examine properties, and to call other methods. Our shopping cart object might have an add to cart method which performs several tasks: it checks product inventory, then it adds the product to the cart, then it increments the cart quantity, and perhaps even removes an item from the inventory. That's a lot of complexity that's being nicely packaged up into a single, simple function. So objects are very useful, especially for managing complexity.
They make our code organized and easier to maintain. It adds clarity to our codes so that it's clear what we're trying to do and reduces complexity. We can write simple rules to allow complex interactions because functions can call other functions which call other functions and so on. And they also allow us to emphasize the data over the procedure. The objects have certain characteristics and they react a certain way based on those characteristics and objects allow us to have modular, reusable code.
If we create a customer object in one project, it very well may be largely reusable in another project and we'll be able to take advantage of all of the intelligence that we've put into that object in the previous project. Now that we have some idea of what objects are and why they're usable, let's get started using them.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project