Article

Object Oriented PHP: Paging Result Sets

Page: 1 2 3 4 5 6 7 Next

The Object Oriented Version

As I explained above, the objective of OOP is to bring the data (in this case, the width and height variables) together with the code for working with them (our two functions for calculating area and perimeter).

Let's modify rect.php so that instead of just defining a couple of functions, it defines a class from which we can create (instantiate) rectangle objects:

<?php  
class Rectangle  
{  
 var $width;  
 var $height;  
 
 function Rectangle($width, $height)  
 {  
   $this->width = $width;  
   $this->height = $height;  
 }  
 
 function area()  
 {  
   return $this->width * $this->height;  
 }  
 
 function perimeter()  
 {  
   return ($this->width + $this->height) * 2;  
 }  
}  
?>

Let's step through this code a few lines at a time, so I can explain it from top to bottom:

We start by declaring that we're writing a class named Rectangle.

class Rectangle  
{

Class names, by convention, should always begin with a capital letter. This is not required by PHP, but by sticking to the established standard like this, your code will be easier for other developers to read and maintain.

Next we declare two variables: $width and $height.

 var $width;  
 var $height;

These aren't just regular PHP variables, however! Since they appear inside a class definition, we're actually declaring variables that will exist inside each object of this class. Remember: we're drawing up the blueprints from which rectangle objects will be constructed. So basically we're telling PHP that every rectangle should have a width and a height. In OOP parlance, variables that belong to objects are called properties. So we've just declared two properties of Rectangle objects.

Up next, a function:

 function Rectangle($width, $height)  
 {

Like with the variables above, functions declared inside a class definition will belong to every object of this class. This function is special, because it has the same name as the class (Rectangle). Such a function is called a constructor, and is called when the object is instantiated, as we'll see shortly.

Constructors are responsible for setting up the newly-instantiated object so that it's ready to use. In the case of our Rectangle class, we need to give the width and height properties (variables) that we declared above their initial values. As you can see above, this constructor function takes two parameters: $width and $height. Let's see how the constructor assigns those parameter values to the corresponding properties in the object:

   $this->width = $width;  
   $this->height = $height;  
 }

Within any function that is part of a class definition, the special variable $this refers to the object to which the function belongs. Thus, in the above code, both occurrences of $this mean "this Rectangle object".

The arrow operator (->) may look scary at first, but it's really quite simple. It's used to get at the variables and functions that belong to an object. So in the above, $this->width refers to the $width variable we declared above -- the width property of this object. Likewise, $this->height refers to the $height variable that belongs to this object.

With this in mind, our constructor function is taking $width (the first parameter passed to the function) and assigning it to $this->width (the width property of this object), and is taking $height (the second parameter) and assigning it to $this->height.

We'll see later how we can use this constructor to create a new Rectangle object with a given width and height. For the time being, let's move onto our (by now familiar) area and perimeter calculation functions:

 function area()  
 {  
   return $this->width * $this->height;  
 }  
 
 function perimeter()  
 {  
   return ($this->width + $this->height) * 2;  
 }  
}

These are very similar to the rect_area and rect_perim functions we declared in the previous section, but you'll notice that these functions don't take any width/height parameters. Like the variables ($width and $height) we declared above, these functions will belong to each object that is instantiated from this class. If we create three different Rectangle objects, each one will have an area and perimeter function of its very own. Just like variables that belong to objects are called properties, functions that belong to objects are called methods.

Now, with this in mind, it should become clear why the area and perimeter methods don't require parameters. The job of these functions is not to calculate the area and perimeter of just any rectangle. Their job is to calculate those values for their rectangle!

So if you look closely at the functions, you'll notice that they use $this->width and $this->height as their width and height values. Once again, we're using $this to mean "the object to which I belong".

To see how all the elements of this class declaration come together to create a working object, let's revise domyhomework.php to use our new Object Oriented approach to rectangles.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links