Join Kevin Skoglund for an in-depth discussion in this video Project introduction, part of PHP: Object-Oriented Programming.
- [Narrator] In this chapter, we're going to apply all of the concepts we've learned about object-oriented programming in PHP to a real world project. You'll still have your OOP sandbox files as reference if you need to review something, but we're going to be starting work in a new directory. First, let me introduce the project to you. The idea is that there's a small, local bicycle shop called Chain Gang. Chain gang is the term for when a bunch of bicycles get in a line so that the bicycles can draft off of the lead bicycle. This bicycle shop sells used bicycles to the public and they maintain their list of used bicycles that are available, in a spreadsheet.
What they want is a way to put that spreadsheet on their website. They want to do it as simply as possible. They don't want to have a database, have an admin area, or any kind of user authentication. They want to just be able to take this spreadsheet and periodically update the list of bicycles available on the website. So what we're going to have them do to keep it simple is just export that spreadsheet to a CSV file. CSV stands for comma-separated values, and it's a very simple file format, where each of the values are separated by commas. And then we're going to use PHP to parse that file and read it in, and used object-oriented programming to convert that data into HTML that we can put on the website.
Then when they want to update their bikes, they just update the CSV file and the website will update automatically. Let's take a peek at the finished product. So this is the Chain Gang website. It's very simple. It just has a couple of pages. There's a home page here, has a link for View Our Inventory and a link for About Us and a big image at the bottom. Let's look at the About Us page and you'll see it just has a bit of text and an image at the bottom. If we click on the bicycle, go back to the main page, and we have View Our Inventory. This is where we're going to be doing our work. The idea is to have a list of the bicycles that are available here.
So you can see how we have that now in a table format. And it's reading these in from a CSV file to be able to output it as HTML. So our goal is to use PHP object-oriented programming to accomplish that. I've already given you a beginning set of files for this project. And the directory is called, chain_gang Let's go ahead and take a look at that inside the Atom text editor so we can see what's inside. You'll notice right off the bat that there are two directories: private and public.
This is a common way to set up PHP projects. And the idea is that the public directory contains all of the PHP files and the images and the style sheets that the public should have access to. The private directory contains the library of code that the public should not have access to. Our code in the public directory still has access to it though, because our code is able to look in the file system like this. Require once, go back a directory, and into the private directory to load up initialize.php Now we're not going to worry too much about about.php and index.php there's nothing out of the ordinary in those.
We're going to focus on bicycles.php You'll notice that the first thing that it does, is it loads up that initialize.php in the private directory. Let's go take a look at that. This is one file that loads up everything that we need to get started. Make sure that everything is available so our PHP code can run well. You know this for example, that turns on output buffering at the start. If we needed sessions, we could turn those on here as well. And it defines a number of PHP constants that we can use; for private path, project path, public path, and shared path.
You'll see in bicycles.php we're making use of shared path right here, in order to find the directory that contains the public header that will display at the top of every page. We also set a variable for WWW_ROOT which is the document root. We can hard code that or find it dynamically. Once we've got all of those defined, then it calls, require once on functions.php This is a file that just contains some basic PHP functions that I use in every one of my projects. We worked on all of these in the PHP essential training.
url for helps us construct absolute URLs. We've got a few functions here that help us to escape values so that we can safely output them for HTML or for use in a URL. We've got some functions to help us render some errors. We've got our redirect function to allow us to redirect to a new page. And we can check if something is a post request or a get request. I'm not even going to use all of those. But it's just a handy library that I use on all of my projects. Let's go back to our initialize.php Right after we load in our functions is when we would want to load in the class definitions to make sure that all of our classes are available.
And we can do that two different ways. We can either load them manually, or we could use auto load to do it. In either case, we're going to put those classes inside this directory called, classes. Right now it's empty, but we're going to be populating that very shortly. And then once we've initialized everything, we have all our functions available, we have our class definitions to find, then in bicycles.php we'll be able to output the actual inventory of used bicycles. We've got a table here that'll do that. Right now it's just got a row of placeholder data. We're going to want to replace this with data coming from the CSV file.
That CSV file is also in the private directory. If we look at that, you'll see that first row in the file is a list of the properties. A comma-separated list of the different properties. These are the same properties that are going to be in our bicycle class, and the same properties that are going to be in that table that we're going to look at. And then for each one of the rows, it has a different bicycles data. Here's a Trek Emonda 2017 bicycle. Cannondale Synapse 2016 bicycle. And so on. There's exactly as many comma-separated values in each row as there are properties in that top header row.
You could poke around the other files if you want to explore and see what everything else is, but it's all pretty standard PHP. I want us to focus on this bicycles.php and how we're able to read from that CSV file and then take a bicycle and put it into that table. Let's begin that process in the next movie.
- 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