Ten minutes to PHP objects
Written by Ian Elliot   
Monday, 30 May 2011
Article Index
Ten minutes to PHP objects
Constructors and Inheritance
An example

 


A button pad HTML object

It is difficult to give a really good example of how to make use of the object-oriented features of PHP – or indeed any object-oriented language. The reason is that the real advantages of the object-oriented approach really only become clear when the project is large and complicated - and large and complicated examples generally aren’t clear!

To give you some idea of how you might go about using the object-oriented approach in PHP let’s build a simple class which generates a particular HTML object.

A common requirement is a set of buttons arranged in a grid – hence our class is called buttonpad. Given that our earlier example involved a calculator you can see that a buttonpad object might well form the basis of a calculator object’s user interface.

The start of the class simply defines some variables that are used:

class buttonpad
{
 public $hor;
 public  $ver;

The $hor and $ver properties are going to be used to store the dimensions of the button grid. You can save the code for the class in a file called “buttonpad.inc” for use in a PHP page.

Getting just a little more advanced we can also make use of an array to store the labels that are going to be displayed on each of the buttons. This array can be accessed to set the labels or methods can be provided to set it automatically:

public $names=array();

The constructor simply sets the size of the buttonpad n x m buttons:

function __construct($n,$m)
{
 $this->hor=$n;
 $this->ver=$m;
}

As an example of a method to set the button labels automatically, the following sets them to 1,2,3,4 and so on reading right-to-left and top-to-bottom:

public function SetSequentialLabels()
{
 $count=1;
 for($y=1;$y<=$this->ver;$y++)
{
for($x=1;$x<=$this->hor;$x++)
  {
$this->names[$x][$y]=$count;
$count=$count+1;
 }
}
}

This complicated-looking function simply stores consecutive values in the array names.

Finally all we need is a method to generate the appropriate HTML:

public function output()
{
 for($y=1;$y<=$this->ver;$y++)
 {
 for($x=1;$x<=$this->hor;$x++)
  {
 print "<input type=button ";
  print "value=".$this->names[$x][$y]." ";
  print "name=B".$y.$x.">\n";
  }
  print "\n<br>\n";
 }
}

The only complicated part of this method is the print statement which builds up an HTML tag something like:

<input type=button value=caption 
name= Browcolumn>

where caption is the value stored in the appropriate location of the $names array and rowcolumn is simply the row and column number that the button is in.

For example, the first button is:

<input type=button value=caption name= B11>

then:

<input type=button value=caption name= B12>
and so on.
If you enter all of this code into the file “buttonpad.inc” you can make use of it in a PHP page using something like:
<?php
include("buttonpad.inc");
?>
<html>
<head>
<title>Buttons demo</title>
</head>
<body>
<?php
$pad=new buttonpad(3,3);
$pad->SetSequentialLabels();
$pad->output();?>
</body>
</html>

Notice that the “include” has to be the first item in the page and that the included file has to start with <?php and end with >>. This might look complicated by notice that the PHP program is just:

$pad=new buttonpad(3,3);
$pad->SetSequentialLabels();
$pad->output();

and this generates a 3x3 labelled grid of buttons.

 

buttons

A button grid generated by a PHP class

 

Of course there are lots of big and small improvements you can make to the class. For example, it seems to make sense to allocate a default labelling for the buttons and this can be best done in the constructor:

public function __constructor($n,$m)
{
$this->hor=$n;
$this->ver=$m;
$this->SetSequentialLabels();
}

It is very easy to create PHP classes that make use of state or session information. This makes it possible to take an object-oriented approach to problems such as user tracking - simply define a User class and create an instance for each user. In the same way you could build a shopping system based on a shopping cart class and so on. And when it comes to database manipulation, objects were just made for the job! The point is that there are times when an object-oriented approach really is justified and other times when you are probably better off just writing PHP script functions.

To access the final example as a php program file,  once you have registered,  click on CodeBin.

If you would like to know more about using objects to generate HTML see:

Object-oriented HTML generation

 

Banner


Ten minutes to PHP

Want to get started with PHP but never found the time? Now you can write your first program in around ten minutes and understand where to go next.



Developing with PHP and Eclipse (Indigo)

Find out how to make PHP development easy with the latest version of Eclipse, Indigo, and how to set up local and remote debugging.


Other Articles

<ASIN:0596157134>

<ASIN:1593271735>



Last Updated ( Monday, 30 May 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.