Introduction to OOP using PHP - Page 3
       by Brian Haveri aka bwh2 | 31 December 2006

In the previous page we talked about class variables and constructor methods, and in this page, we pick up from where we left off earlier.

Using Arrays to Speed Up Coding
Right now, it's tedious to set each property using a set method - there must be an easier way. That's where associative arrays combined with our constructor come in handy. Let's create our user object and give it several properties upon instantiation:

I have kept the set methods in case we want to change the properties, but now the properties are established up front via __construct. That is, we passed our $attribs array to the object on instantiation, which in turn passed $attribs to the constructor automatically. The advantage to using associative arrays for this purpose is that we don't need to explicitly set all of our variables with the methods. This is extremely handy when integrating PHP with MySQL data, which we will cover soon. But first, we need to learn a little bit about method interaction and object grouping.

Method Interaction
At this point, our methods do not interact. Let's add a little functionality to our example by converting full state names to the appropriate abbreviations.

In this example, we used a full state name to get its corresponding abbreviation. We did this using a class with a static variable. Static variables generally do not change and can be accessed without methods. These variables act more as reference and can be useful for things like your database connection settings and other site wide variables. Static variables are usually named with all capital letters.

Next, we will learn to integrate our object oriented PHP with MySQL.

Integrating OO PHP with MySQL
In this section, we will kill two birds with one stone. Not only will we learn how to integrate OO PHP and MySQL, but we will learn how to group objects. In this case, you will create a UsersGroup class which will contain an array of User objects. Each of those User objects will be created using a row from a MySQL query. To try this practical example, you will first need to create a Users table in a MySQL database. I am using a MySQL database named `kirupa_oop`. Use the queries below to create the table and insert the sample data.

This very simple table just has two columns: `user_id` and `user_name`. You may wish to add your own name to this table to spice things up.

As promised, we will create a UsersGroup class. This group will contain an array of User objects, each of which is created using data from our MySQL table. All of my tables are on a database named `kirupa_oop`. If your database is not named `kirupa_oop`, you must change the database name in the Settings class shown below. Here is our UsersGroup class in action:

The first thing to note is that we are holding our database connection and table name information inside our Settings class, within static variables. In the event that we need to change our database name, table names, database usernames, etc. we will only do it in our Settings class (which is preferably in a separate, included file). Our User class is nothing new - it only holds the name property and still receives an array of data via __construct.

Let's look at the code for the UsersGroup class. If you have connected to a MySQL database with PHP before, the connection should be nothing new. The only difference is that we use static variables for our connection settings. We do the same for our table names because within double quotes, our Settings class static variables cannot be parsed. That is, just using Settings::$TABLES['tbl_users'] directly in our double-quoted SQL query will cause an error.

Our SQL query produces an array of rows, which we assign to $result. Using a while loop, we go through our $result array. Each element in $result is an array of values (like id and name). Again, this should look very familiar to anyone who has used PHP to access a MySQL database. Now comes the OOP part: we then pass each row ($row) from our MySQL query to our addUser method. First, the addUser method checks if it's being passed an array of data (like $row) or a whole User object. In this case, we are passing an array of data. Using that array of data, addUser creates a User object ($noob), then adds that user object to the group array ($group).

Note about column names: When our User object is created with array data, the constructor is looking for an array element called name. To fulfill this request, we use a SQL alias, which can be seen in $sql. If we did not alias user_name AS name the $row variable would not hold an array element called name but rather user_name. Thus, it would not correctly populate the name variable in our User object. I prefer to keep customization in my SQL rather than my PHP class methods because I am more likely to reuse PHP class methods than SQL queries. For instance, if I create a Dogs class, I would also want a getName method and constructor. Rather than modify several PHP methods to support dogs instead of users, I would rather modify one SQL query. Alternatively, you could also rename your user_name column to just name. This would maximize reusability, but may hurt your ability to understand more complex SQL queries and database designs.

You might be thinking this class is overly complex. After all, you can probably code all of this in 15 lines within a regular PHP page. So where's the advantage? Well, look at how we use our objects:

Really, that's 4 lines of code to echo out all of the users in our table. All of the database connection, querying, and real code is sitting behind the scenes. Remember, we are going to include our classes as separate files. On top of that, our classes are very reusable (aka modular). Our code is beautifully generalized and hidden from plain sight. It should also coded semantically, so it's easy to understand. Debugging and maintenance time should be decreased significantly. In the end, we're going to reuse this code for many sites and save tons of time and energy.

So that's a basic user table accessed with a UsersGroup class. But what if we want some users to be Administrators and others just plain Users - how can we do that? For the answer, let's continue on with the topic of inheritance.

Onwards to the next page!

1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.