llbbl
09-01-2004, 08:34 AM
The basic idea of object-oriented programming is to bind data and functions in convenient containers called objects. For instance, in Chapter 7 we'll show you how to standardize the look of your own web pages through an object called a template. Your PHP code can refer to this object through a variable; we'll assume here you've decided to call the variable $template. All the complex implementation of templates is hidden: you just load in the proper package and issue a PHP statement such as:
$template = new HTML_Template_IT("./templates");
As the statement suggests, you've just created a new object. The object is called $template and is built by the HTML_Template_IT package—a package whose code you don't need to know anything about. Once you have a template object, you can access the functionality provided by the HTML_Template_IT package.
After various manipulations of the $template object, you can insert the results into your web page through the PHP statement:
$template->show( );
The syntax of this statement is worth examining. As the parentheses indicate, show( ) is a function. However the -> operator associates show( ) with the object variable $template. When the function show( ) is called, it uses the data that is held by the $template object to calculate a result: put another way, show( ) is called on the $template object.
The functions that you can call depend on the support provided by the package—the show( ) function is provided by the HTML_Template_IT package and can be called on HTML_Template_IT objects such as $template. In traditional object-oriented parlance, show( ) is called a method or member function of the HTML_Template_IT object.
HTML_Template_IT is called a class because you can use it to create many similar template objects. Each time you issue a new statement you are said to create an instance of the class. Thus, the $template object is an instance of the HTML_Template_IT class.
We've shown how to use objects created by other packages. However, to understand objects better, it's time to define a class of our own. Example 4-1 shows a simple class invented for the purposes of this chapter that's called UnitCounter. The UnitCounter class provides two trivial features: we can use a UnitCounter object to keep a count of things, and to calculate the total weight of the things we have counted. Later in this chapter, and in Chapter 14 we use the UnitCounter class, together with other classes, to develop a simple freight-cost calculator.
Example 4-1 shows how the class UnitCounter is defined using the class keyword. The UnitCounter class defines two member variables $units and $weightPerUnit, and two functions add( ) and totalWeight( ). Collectively, the variables and the functions are members of the class UnitCounter.
Example 4-1. Definition of the user-defined class UnitCounter
<?php
// Definition of the class UnitCounter
//
class UnitCounter
{
// Member variables
var $units = 0;
var $weightPerUnit = 1.0;
// Add $n to the total number of units, default $n to 1
function add($n = 1)
{
$this->units = $this->units + $n;
}
// Member function that calculates the total weight
function totalWeight( )
{
return $this->units * $this->weightPerUnit;
}
}
?>
http://www.oreilly.com/catalog/webdbapps2/index.html
Anyone take advantage of this?
$template = new HTML_Template_IT("./templates");
As the statement suggests, you've just created a new object. The object is called $template and is built by the HTML_Template_IT package—a package whose code you don't need to know anything about. Once you have a template object, you can access the functionality provided by the HTML_Template_IT package.
After various manipulations of the $template object, you can insert the results into your web page through the PHP statement:
$template->show( );
The syntax of this statement is worth examining. As the parentheses indicate, show( ) is a function. However the -> operator associates show( ) with the object variable $template. When the function show( ) is called, it uses the data that is held by the $template object to calculate a result: put another way, show( ) is called on the $template object.
The functions that you can call depend on the support provided by the package—the show( ) function is provided by the HTML_Template_IT package and can be called on HTML_Template_IT objects such as $template. In traditional object-oriented parlance, show( ) is called a method or member function of the HTML_Template_IT object.
HTML_Template_IT is called a class because you can use it to create many similar template objects. Each time you issue a new statement you are said to create an instance of the class. Thus, the $template object is an instance of the HTML_Template_IT class.
We've shown how to use objects created by other packages. However, to understand objects better, it's time to define a class of our own. Example 4-1 shows a simple class invented for the purposes of this chapter that's called UnitCounter. The UnitCounter class provides two trivial features: we can use a UnitCounter object to keep a count of things, and to calculate the total weight of the things we have counted. Later in this chapter, and in Chapter 14 we use the UnitCounter class, together with other classes, to develop a simple freight-cost calculator.
Example 4-1 shows how the class UnitCounter is defined using the class keyword. The UnitCounter class defines two member variables $units and $weightPerUnit, and two functions add( ) and totalWeight( ). Collectively, the variables and the functions are members of the class UnitCounter.
Example 4-1. Definition of the user-defined class UnitCounter
<?php
// Definition of the class UnitCounter
//
class UnitCounter
{
// Member variables
var $units = 0;
var $weightPerUnit = 1.0;
// Add $n to the total number of units, default $n to 1
function add($n = 1)
{
$this->units = $this->units + $n;
}
// Member function that calculates the total weight
function totalWeight( )
{
return $this->units * $this->weightPerUnit;
}
}
?>
http://www.oreilly.com/catalog/webdbapps2/index.html
Anyone take advantage of this?