Creating Your First PHP Application: Part 1
Editor s Note: This is a guest post from Brian Muse, our lead developer on You Rather. He ll be guiding you through a three part journey of PHP applications over the next few days.
This tutorial is intended for readers who know the very basics of PHP and Object Oriented Programming (OOP) and would like to create a basic web application.
To make this a little bit clearer, I ve split this tutorial up into three separate posts. Each post will cover a major step in setting up a basic PHP web application.
We ve got a lot of ground to cover. Here s a general outline about what to expect from each post in this series:
Part 1 Setting up the project and creating your first class
- Creating an outline of the project
- Setting up your files and folders
- Creating a class to handle database operations: DB.class.php
Part 2 Building the rest of the backend
- Creating a User class
- Creating a UserTools class
- Registration / Logging in / Logging out
Part 3 Building the front end
- Form Handling
- Displaying session data
Setting up the Project
Creating a Road Map
It s always a good idea to know where you re going. Before you start creating and coding files it s best to set your goals, map out the project and make decisions about your folder structure and what files you ll need to make to accomplish your goal. The goal for this project is fairly simple: Create a basic PHP web application with user registration, the ability to log in and out and a way for users to update their settings.
Files and Folder Structure
An OOP PHP project utilizes classes and objects to perform many of the operations that the application requires. When planning, you should think about what classes you will need. For this project we ll be making three classes. The first is the User class, which will hold information about a particular user and a basic save() function. Another class, UserTools will contain functions that have to do with users, such as login(), logout(), etc. The final class is the first class we ll be coding: the database class. This class will handle connecting to the database, updating, inserting new rows, retrieving rows, and more.
Aside from classes, we ll utilize a file called global.inc.php. This file will be called on every page and will perform general operations that we commonly require. For example, it is this file that will handle connecting to the database on each page.
The rest of the files are the pages the user will navigate around. These include index.php, register.php, login.php, logout.php, settings.php and welcome.php.
The final directory structure should look like the image below:
Creating your database and users table
You must have MySQL installed on your server to continue. You ll first have to create a new database for your application. Within that database to create the users table we ll be using for this tutorial, use the following SQL:
The second function is called processRowSet(). The purpose of this function is to take a mysql result object and convert it to an associative array, where the keys are the column names. The function loops through each row in the mysql result and the PHP function mysql_fetch_assoc() converts each row to an associative array. The row is then pushed onto an array which is ultimately returned by the function. This formatting makes the data far more readable and easier to use.
There is a second argument called $singleRow which has false as a default value. If set to true, only a single row will be returned instead of an array of rows. This is useful if you re only expecting a single result to be returned (for example when selecting a user from the database by using their unique id).
The final three functions perform basic MySQL functions: select, insert, update. The goal of these functions is to minimalize the amount of SQL that needs to be written elsewhere in the application. Each basically builds an SQL query based upon the value passed in and executes that query. In the case of select(), the results are formatted and returned. In the case of update(), true is returned if it succeeded. In the case of insert(), the id of the newly inserted row is returned.
Here is a sample of how you might update a user in the database using the update() function: