Join Kevin Skoglund for an in-depth discussion in this video Introducing the concept and basics of OOP, part of PHP with MySQL Beyond the Basics.
I would like to start out this chapter on object oriented programming or OOP by first talking about the concept and the basics of object oriented programming. PHP is the scripting language or may also be also called a procedural language. It's not really a true object oriented language. Instead, it's a scripting language that has some object oriented features built into it. PHP 4.0 first gave a few limited object oriented features. It really wasn't until version 5.0 that we had full object oriented support in PHP.
Do you need object oriented programming? If scripting and procedures are all you want PHP to do, then you probably don't need OOP. In fact, it might even be a bad choice. After all, if it ain't broke, don't fix it! But if you are pushing PHP harder than that and asking it to do a lot of complex work, then OOP may be exactly what you need. Don't let any preconceived notions about what a scripting language can or should do, limit you from the possibilities of object oriented programming in PHP, and most of all, don't be afraid of it. Here is my rule of thumb. I always say that for a simple site, object oriented programming adds unnecessary complexity, but for a complex site, object oriented programming adds necessary simplicity.
Now the next question you may be asking yourself is what are objects? Well, in the simplest way, objects are simply grouping code together by a common theme. So if you think back to some of the work that we did in the Essential Training, we had a lot of different functions related to working with forms. We could have rolled all of those up into a form object and then our code would stay grouped together and we would have been able to find all of those form functions in our form object. Now there is a more complex way that we can use them as well, which is not just to group them together, but actually to abstract the structures in our code into objects. In other words, to make our software objects seem very similar to real world objects. And this makes sense because a lot of what we're doing on the web, we think of as objects. We have a page, a page could be an object or a form, a form could be an object.
Those could be physical objects in the real world in the same way or we could have customers, students, shopping carts. Those are all objects and we can hold them in our head and think about them as objects. Objects have not only functions that allow them to do things, but they have attributes as well. So we would have a student who has a first name and a last name or a shopping cart that has a total quantity in it. Those are attributes of each of those objects, and it's easy for us to think about because we're used to thinking of objects in our head in that sort of way.
It will become clearer what objects are as we start working with them, but let's first take a look at some typical PHP code that we might have in a project we're working on. We might have a student whose first name is John, his last name is Doe, we might have another student Mary and Smith. Then if we wanted to find out a student's full name, we would have a function where we could pass in those two values, Mary and Smith, and it would return Mary Smith to us with the space in between them. We have been doing that kind of thing before, but we can already see how this is going to start to break down if we start having a lot more complexity.
This is just where we're dealing with two students and a simple function to put their name together. What if we had a lot of functionality then? What if we wanted to know the courses that the student is in? I can have a simple array that defines what each of those is, but what if each of those has some complexity to it? What if I don't want to just have student one's course be English but the English class that starts at 1 o'clock on a Wednesday. Then we start having a lot more complexity there and objects are going to serve us well because they are going to allow us to pull all that complexity into a very simple data structure that we can work with.
So why are objects going to be useful for us in programming? They are going to give us better code organization and maintainability. If we group all of our functions and variables related to forms into something that's a form object, then we'll know right where to go when we need to make a change to the forms. If we roll all of the codes related to a student together into a single student object, then when we want to make a change to the way that that code works, we know right where to go. We go straight to that object and we make the change. It also adds clarity and reduces complexity and that's because we're able to think about these complex bits of data, all of the functions related to students, all of those attributes, however much complexity we give to it, we can think of as a student and student has all of that functionality built into it.
So it becomes very clear, we have a student and a student object. It can be asked questions, it can be asked to do things, it can be asked to return data to us, but without us having to navigate all that complexity each time. The simple rules that we put into objects can actually allow for complex interactions. Let's imagine for a moment that we have a classroom and we have a student. We can issue a simple statement like put the student in the classroom and the objects themselves can have complexity built into them that says, well, before you do that, check and make sure that the class is not full, make sure that the student doesn't have a class that conflicts with the time of this class, for example. Then once it puts the student in there, it can update the total class count and the class can keep track of how many students are in it.
So all that complexity can be built in and all we have to do is say, put the student in the class and those simple rules will all kick in. And because of that it's going to emphasis the data over the procedure. The procedure is going to be already preprogrammed for us inside these objects and we can really work on just the data and how the data interacts. Objects are also going to give us code modularity. Because the code is going to be broken into discrete sections, we can go in and work on one section at a time and not worry about hurting something else. This is great if we have a team of developers, because different developers are going to be working on different sets of objects and not interfere with each other; the code will be separated. And last of all is code reusability. If we write a complex shopping cart object, then all of that functionality is there when we take that same shopping cart object and use it for another client.
And last of all, objects are well suited for databases. If we have a table of students, then each of those students can be an object. If we have a table of classrooms, then each of those classrooms can be an object. If we have a table of cars, each car can be an object and so on. So object oriented programming is going to a nice fit for working with databases. Now I think that's enough background on the concepts and advantages of using objects. I think it will start to make a little more sense as we start to actually code and that's what we're going to do in the next movie where we start defining classes.
- Defining and using classes, methods, and attributes
- Understanding class inheritance and access modifiers
- Working with files and directories
- Uploading files to a server via forms
- Sending emails with PHP
- Using pagination and View templates
Skill Level Intermediate
<div>Q: When I try to launch phpMyAdmin in WAMP, I get the following error: </div> <div><strong>#1045 - Access denied for user 'root'@'localhost' (using password: NO). </strong></div> <div>How can I fix this problem?</div>
<div>A: The error is telling you that MySQL wants a password. (Not WAMP or PHP.) You will need to configure phpMyAdmin to send MySQL the correct </div> <div>password ("using password: NO" indicates that you are not sending any right now). The phpMyAdmin setup is shown in the tutorial and the online </div> <div>documentation can help if you get stuck: <a href="http://www.phpmyadmin.net/documentation/" target="_blank">http://www.phpmyadmin.net/documentation/</a></div> <div><br /> </div>
Q: This course was updated on 03/19/2015. What changed?
A: We updated several movies to use the newer MySQL database adapter (mysqli) and added a new movie on late static binding in chapter six, "Foundations of an OOP Project."