Lessons: 18Length: 2.3 hours
ImageImage

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 What Is OOP?

In this lesson we’re going to have a look at what object oriented programming is exactly. This may seem superfluous, but you will see that understanding just a few of the core concepts behind OOP will really get you a long way.

2.1 What Is OOP?

Hello, and welcome to PHP OOP fundamentals. In this lesson, we're going to have a look at what object-oriented programming is exactly. Now this may seem superfluous, but you will see that understanding just a little of the core concepts behind OOP, will really get you a long way. So what is object oriented programming anyway? Well it certainly is not the way most of us learn PHP. Let's face it. PHP is one of the easiest programming languages to pick up, and a lot of that has to do with the fact that PHP is a procedural language by origin, and procedures are something that we can easily understand. You start at square one, and take one logical step at a time, until you reach your destination. I'll show you what I mean. Now, say you want to fetch some blog posts from a database. Now, this could be a procedural way to do that. I'm just gonna paste in some code, because it's for reference only, anyway. So first thing you could do is set up a database connection. And then you would typically want to run a query, and then last of all you would want to display some post titles. Now, for a database connection you will need some credentials, and then you would typically try to connect to the database, like so. And if connection failed, you would want to throw an exception error. And in this case we'll just throw to the screen. Now, dumping sequel errors to the screen is bad practice of course, but this is for demonstration purposes only. Now, that's a whole bunch of code, and that's just for creating a database connection. And now for the query. Typical old school query is writing it by hand. Something like this selects all from post, where pub date is less than now. And we'll order by pub date in a descending manner. And then we'll typically try to run that query. And if it fails, I'll just throw an exception to the screen, as well. And then for the final step, we'll typically see if we do have some results, and then if we do, we'll just run through that results and display the title. So this is a typical procedural call. Now this code works perfectly fine. It's also blazingly fast. But a procedure is typically one big lump of code. You can't take out anything, or it will all break. So how can we improve on this code? Well, think about it. What does your application do? Well basically, it juggles around chunks of data. A user could be such a chunk, or a blog post, or a comment. And these chunks are all used and reused on multiple pages. Sometimes together, but sometimes separately. Now wouldn't it be much more logical, to build your app around those separate chunks of data then? Well, yes. And that's basically what object oriented programming does. In object oriented programming, you would typically instantiate an object. So for instance, if we're talking about blog posts, then we could say post is new post. Let's say this is a post model. And for all of you who have worked with frameworks before, you're probably familiar with the term models. If you're not, don't worry, we'll get to that in a later stage. And now fetching all posts, could be as simple as something like Post, Fetch All, and then we could simply run though this, and say that for each post as post, we'll just echo that to the screen. Maybe wrapped inside of an H1 tag like so, post, title and then close that out. Now these four lines here replace all of this code here. So to wrap it all up object, oriented programming is, simply pu,t programming that is oriented around separate chunks of data or objects. Now these objects represent items in the real world, like blog posts. Or maybe users. They could also represent a database connection, or a log in service. In a perfect world all these objects do not need to know about each other. They can exist separately, and you can juggle them around to your liking. If you need to add an e-mail filter users, well just change the user object, and your app won't break. And one of the biggest pros of object oriented programming is, to get rid of all this interdependencies. Because in this code, think about what would happen if you had to change to a non-PDO source. You would not only have to rewrite your connection everywhere, but you would probably also have to change every instance where you run a query. Where you do a simple thing like ($result->rowCount() because this is a typical PDO statement. Well that was a lot of theory. In the next lesson, we'll start coding OOP style. I'll see you there.

Back to the top