 
Classes/Paging
Code Re-Use
Until now we have reused code mainly be creating functions and placing them in include files. This works fine for most PHP applications. We are thus able to develop more quickly, by including files with code we had already written, and could “see” the code on our page more easily by eliminating code from our working pages.
We also benefit by being able to “fix” or adapt the code in our include file, and to have the results of our efforts effect all pages that call the include. A more advanced way to re-use code is to create a class, and pass data in and out of our class.
We need to learn about classes because a great deal of useful code will come to us in the form of classes.
Classes
The word "Class" is a reference to object oriented programming (OOP). A "Class" is a blueprint, or prototype, that defines the properties (qualities) and methods (functions/operations) common to all objects of a certain type. A class can be thought of as an advanced function, in fact, classes can include several functions. Our Class can expose (share) these functions and perform actions based on variables passed into the class. When we call our "class" we create an instance (a particular occurence) of an object, which is defined by the class from which it sprang. Seabiscuit is a particular horse (an instance of an object) that sprang from the "horse" class! (the model or template defining all horses)
Another example: let’s think about an automobile class. Such a class could have a property (quality or characteristic) called “color”. All objects created based on this class would have such a property, but some objects set this property to “red”, others to “blue”, and so on. This means that the class only holds a definition, and the object holds the actual value (red or blue).
You can declare a class by using the “class” keyword:
class automobile_class
{
public $color; //the color of the car
public $max_speed; //the maximum speed
public $price; //the price of the car, in dollars
function is_cheap()
{
return ($this->price < 5000); //returns TRUE if the price is smaller than 5000 dollars
}
}
After the declaration of the class, you can see the variables used within the class, which are called properties. These are declared using the "public" statement, which allows the data stored in them to be available outside the class. While they can be defined anywhere within the class, you should really define them at the very top, so you can better see the class properties.
Functions within a class are called methods. They're used to manipulate the class properties and produce results. In this simple method you can see that when we use a class method or property, we must use the “->” operator, which is used to reference a property or method in any class, either from the inside or outside. The keyword “this” tells the class to reference itself. PHP then knows the property or method is internal (contained) inside the same class.
While a class only exists in code and is considered to be a blueprint, an object exists in memory and is a working instance (example) of a class. An instance of an object is created using the "new" statement along with the name of the class the object is based on. Let’s return to our automobile class:
$car_object = new automobile_class();
$car_object->color = "red";
$car_object->price = 6000;
if($car_object->is_cheap())
{
print "This car is cheap!";
}else{
print "This car is expensive!";
}
We use the "->" operator to access and modify object’s properties. After that, we use the same operator to call a method. Perhaps the greatest benefit of object-oriented code is reusability. Because the classes used to create objects are self-enclosed, they can be easily pulled from one project and inserted into another. Additionally, it is possible to create child classes that inherit and/or override the characteristics of their parents. This technique allows you to create more complex and specialized objects. Even if you start with a small class, you can develop it to a complex class by time, with adding more properties and objects to its children classes.
Constructor
If you wish to have the object load initial properties when it is created, you place code into a special function called a constructor. This function runs when an instance of the object is created. In PHP version 5 the constructor is named __construct() (note the double underscore!). In previous versions of PHP, the constructor was given the same name as the class. (This is done in several other languages).
In the following example we create a constructor and output a string and set a variable when an object is created based on this class:
class automobile_class
{
private $negotiable_price;
public $price; //the price of the car, in dollars
function __construct()
{
print "Object created!";
this->$negotiable_price = FALSE;
}
}
Expanding PHP Classes
While you can always modify a class to add more properties and methods, you can also create a more complex class based on a simple one. Extending existing classes by creating new classes inherit their parent class properties and methods. This is also called a "parent-child" relationship. The parent class must be defined before the child class, so the order in which the classes are defined is important. Let's get back to our automobile class, and create a new class based on the original one:
class used_automobile_class extends automobile_class
{
var $owner;
var $is_price_negotiable;
function write_negotiable()
{
if ($this->is_price_negotiable)
{
print "The price is negotiable!";
}else{
print "The price is NOT negotiable!";
}
}
}
Note the keyword extends, which identifies our new used_automobile_class. Our new class inherits the all of the properties and methods of "automobile_class", and adds some new ones. While you can always add new properties and methods, it is not possible to remove any of the properties or methods defined in the parent class. You can override them, though. This means that a child class can redefine a method of its parent class, and any new objects created based on the child class will use the child’s redefined method.
There are times when you start writing a class from scratch, add some more properties as needed and create this large class, but then you want to get back to one of the initial structures and redefine it some other way. This would not have happened if you had created children classes that expanded the original class.
Once you are familiar with classes, be sure to go to phpclasses.org, and other web sites and utilize all the great code that is available in class form!
Paging
When you are presenting records (for example our categories page) that number into the dozens or possibly the hundreds you will need to consider spanning your data across multiple pages. Google, Amazon and other web sites use a method of showing "Next" and "Previous" links on the bottom of a page, as well as numbered links to allow a user to select a single page.
Pager Class: The following example builds a PHP class, and allows easy access to paging. You can either use simple characters such as < and > for your ‘previous’ and ‘next’ icons, or use images.
The Pager class creates simple records paging by deconstructing the existing SQL statement and adding MySQL limits to the statement. Once the Pager object is loaded with the SQL statement, a method named 'showTotal()' returns the possible number of records, and another named 'showNav()' places the Paging Nav (next & previous arrows, etc.) on the page.
Calling the constructor allows a default configuration, which applies simple text 'arrows'. Upon creation the developer can identify a different number of records per page, and implement images for arrows or text in any combination. Here is the minimal implementation using the default 'first' 'prev', 'next', 'last' arrows, and declaring 10 records per page:
$myPager = new pager(10);
The next example instantiates the pager object with 20 records and images for previous & next arrows, and no first & last arrows:
$myPager = new pager(20,'','<img src="images/arrow_prev.gif" border="0" />','<img src="images/arrow_next.gif" border="0" />','');
Note the use of single quotes, '', to indicate no 'first' or 'last' icon above.
Once the pager is instantiated, you are required to run your SQL statement through the loadSQL() function:
$myPager = new pager(10); //create new pager object
$sql = $myPager->loadSQL($sql); //adapt existing SQL statement
Since MySQL limits the number of records returned, this function will disassemble the SQL statement and re-assemble it to retrieve the total number of records per page. The adapted SQL statement is returned to be used by the page.
IMPORTANT: The pager needs to adapt the SQL BEFORE the SQL statement is used by the page. You would re-use the variable named $sql (for instance) in the SQL call on the page. See a new version of the muffinlist.php example below to see the Pager implementation in action.
|