Ten minutes To PHP Objects
Written by Mike James   
Thursday, 18 November 2021
Article Index
Ten minutes To PHP Objects

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

public function output()
 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>


<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:

<title>Buttons demo</title>
$pad=new buttonpad(3,3);

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);

and this generates a 3x3 labelled grid of 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)

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


Introduction to PHP



  1. Getting started
    Getting Started With NetBeans PHP - Local Projects
  2. What PHP Does
  3. PHP Variables and Expressions for Complete Beginners
  4. PHP Control Structures 1 - if and else
  5. PHP Control Structures 2 - switch and elseif
  6. PHP loops
  7. Advanced loops
  8. Functions
  9. Ten minutes to PHP objects
  10. PHP Inner Functions And Closure
  11. NetBeans Remote Projects and debugging

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


PHP Inner Functions And Closure

PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP fun [ ... ]

Object-Oriented HTML Generation In PHP

PHP is a programming language specifically designed to generate HTML pages, but one of the topics that is rarely discussed is exactly how to do this. In this article the idea that HTML generation shou [ ... ]

Other Articles

Summer SALE Kindle 9.99 Paperback $10 off!!




or email your comment to: comments@i-programmer.info





Last Updated ( Thursday, 18 November 2021 )