if ($height) $this->tag .= 'height="' . $height . '" ' ;
$this->tag .= ">" ;
return $this->tag ;
}
function ColumnOn($colspan=1, $align='left', $width="", $rowspan="",
$bgcolor="", $class="", $valign="", $height="") {
$this->tag = '<td ' ;
if ($align) $this->tag .= 'align="' . $align . '" ' ;
if ($colspan) $this->tag .= 'colspan="' . $colspan . '" ' ;
if ($width) $this->tag .= 'width="' . $width . '" ' ;
if ($rowspan) $this->tag .= 'rowspan="' . $rowspan . '" ' ;
if ($bgcolor) $this->tag .= 'bgcolor="' . $bgcolor . '" ' ;
if ($class) $this->tag .= 'class="' . $class . '" ' ;
if ($height) $this->tag .= 'height"' . $height . '" ';
if ($valign) $this->tag .= "valign='" . $valign . "'>" ;
if (!$valign) $this->tag .= "valign='middle'>" ;
return $this->tag ;
}
function ColumnOff() {
$this->tag = '</td>' ;
return $this->tag ;
}
function RowOff() {
$this->tag = '</tr>' ;
return $this->tag ;
}
function End() {
$this->tag = '</table>' ;
return $this->tag ;
}
} // end class table
function Input($InputType, $EntityName, $value="", $align="center", $size="",
$id="", $align="center", $readonly="", $class="",
$onType1="", $onAction1="", $onType2="", $onAction2="",
$onType3="", $onAction3="") {
$this->tag = '<input type="' . $InputType . '" name="' . $EntityName
. '" size="' . $size . '" ' ;
if ($align) $this->tag .= 'align="' . $align . '" ' ;
if ($id) $this->tag .= 'id="' . $id . '" ' ;
if ($value == "on"){
$this->tag .= ' checked ';
} elseif ($value){
$this->tag .= 'value="' . $value . '" ' ;
}
if ($class) $this->tag .= 'class="' . $class . '" ' ;
if ($onType1) $this->tag .= $onType1 . '="' . $onAction1 . '" ' ;
if ($onType2) $this->tag .= $onType2 . '="' . $onAction2 . '" ' ;
if ($onType3) $this->tag .= $onType3 . '="' . $onAction3 . '" ' ;
if ($readonly) $this->tag .= 'readonly ' ;
$this->tag .= ">" ;
return $this->tag ;
}
function Textarea($name, $cols, $rows, $value="", $align="left", $class="",
$readonly="", $onType1="", $onAction1="", $onType2="", $onAction2="",
$onType3="", $onAction3="") {
$this->tag = '<textarea name="' . $name . '" cols="'
. $cols . '" rows="' . $rows . '" ' ;
if ($align) $this->tag .= 'align="' . $align . '" ' ;
if ($class) $this->tag .= 'class="' . $class . '" ' ;
if ($onType1) $this->tag .= $onType1 . '="' . $onAction1 . '" ' ;
if ($onType2) $this->tag .= $onType2 . '="' . $onAction2 . '" ' ;
will accept a first name with a display of 30 characters, a last name with a display of 40
characters, and a comment area of 8 rows and 40 columns. Again, we will keep it simple
in design and process, just to get the points of OPP across. Hopefully, you will be able
to extrapolate this context into a fully operational OOP library.
So let’s get back to the design of the web page form. Figure 6-1 shows the simple form
for which we will be writing the code below.
Magic Methods
You may notice that in the HTML class definition there is a function called
__construct. This is a special method that you can define for each class that is triggered
(executed) each time the class is instantiated. What we are doing here is establishing
the basic top of an HTML page at the same time that we are creating the object in
memory. Additionally, we are passing in a value that will be used for the page title. This
__construct method is actually looked for and executed each time a class is instantiated,
whether or not the code for the method is actually written. If the method is not written,
it will be called, but nothing will happen visibly.
The automatic method we use here (__construct) is part of a collection of predefined
methods known as magic methods. These magic methods are all inherently defined
within each class that you build within PHP. Even if you don’t write the code for them
yourself, they still exist and the PHP parser will use them or call them as needed,
Magic Methods | 65
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
although the definition will be that of the default behaviour PHP has assigned to it.
Another magic method that is quite often used is the destructor method (__destruct).
It is called when an object is removed from active use or when a script ends. If you want
something special to be performed as the object is being destroyed, create this method
within your class definition and add your desired code to it.
You may notice in the HTML class code that there is a __construct
method
echoing out the content of the top of a web page
(<HTML><HEAD>, etc.). This is merely an example of the use of one of the
$webpage .= $MyTable->ColumnOff();
$webpage .= $MyTable->ColumnOn();
$webpage .= $MyForm->Input("text", "lname", "", "", 40);
$webpage .= $MyTable->ColumnOff();
$webpage .= $MyTable->RowOff();
$webpage .= $MyTable->RowOn();
$webpage .= $MyTable->ColumnOn();
$webpage .= $MyForm->InputLabel("Comments","comments", true);
$webpage .= $MyTable->ColumnOff();
$webpage .= $MyTable->ColumnOn();
$webpage .= $MyForm->Textarea("comments", 40, 15);
$webpage .= $MyTable->ColumnOff();
$webpage .= $MyTable->RowOff();
$webpage .= $MyTable->RowOn();
$webpage .= $MyTable->ColumnOn(2, "center");
$webpage .= $MyForm->Input("submit", "submit", "Save Entry");
$webpage .= $MyTable->ColumnOff();
$webpage .= $MyTable->RowOff();
$webpage .= $MyForm->form_end();
$webpage .= $MyTable->End();
$webpage .= $HTMLPage->page_end() ;
echo $webpage ;
As you can see, the code uses an output variable called $webpage to store all the returned
values from the class methods that are called to construct this page. On each page, there
Objects in Action | 67
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
are functions (methods) of the class that you cannot use. In the html class, for example,
only the constructor and the page_end methods are used. This is normal behavior—you
don’t always use every screwdriver in your toolbox for each odd-job task.
Notice that many of the method calls do not pass their defined parameters. This is a
a class library that will be used in many different projects or if you are making a com-
mercially available library. This protects your code from being tripped up in any way,
either by improper use or by directly accessing its properties from outside the class
itself. The protected scope is rarely used unless you have a heredity tree (inherited
classes) that can benefit in some way. The public scope is the default scope if none is
68 | Chapter 6: Objects
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
declared, and it is used best on methods that allow the class to interface with the outside
world. The best way to share information to an outside entity that is making use of
your class is with the public get and set methods (a.k.a. accessor methods) that act on
the privately declared class properties. We’ll look at this more closely in the following
section.
Getters and Setters
The last major portions of the OOP model that we’ll look at in this chapter are the
get and set methods for the class properties. This concept allows for a more protected
interface within the class itself. Each class property has its own get and set methods,
and the only way to affect each property is through the use of these accessor methods.
Here is a person class that has the properties firstname, lastname, and gender, with
get and set methods for all three properties:
class person {
private $firstname ;
private $lastname ;
private $gender ;
public function getFirstname() {
return $this->firstname;
}
public function getLastname() {
return $this->lastname;
}
public function getGender() {
echo "<br/> Gender: " . $newPerson->getGender() ;
The above code will produce the following output:
the Person class currently has these values:
First Name:Peter
Last Name: MacIntyre
Gender: male
As you can see here, there is no direct call to any of the three properties of this class.
For example, we cannot write the following:
echo $newPerson->lastname ;
There are many other aspects of OOP PHP that we have not touched on here—topics
like inheritance, interfaces, object cloning, late static binding, and so on. My purpose
in this chapter was simply to demonstrate the power and simplicity of the OOP ap-
proach to web programming, and to give a concise example of it in practical use. For
a full and thorough explanation of OOP in PHP, I recommend the book Object Oriented
PHP (No Starch Press) by Peter Lavin.
70 | Chapter 6: Objects
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.