Objects
The fifth PHP data type is the object. You can think of an object as a variable that is instantiated from a kind of template otherwise known as a class. The concept of objects and classes is integral to the notion of object-oriented programming (OOP). Contrary to the other data types contained in the PHP language, an object
must be explicitly declared. It is important to realize that an object is nothing more than a particular instance of a class, which acts as a template for creating objects having specific characteristics and functionality. Therefore, a class must be defined before an object can be declared. A general example of class declaration and subsequent object instantiation follows:
class appliance {
var power;
function set_power($on_off) {
$this->power = $on_off;
}
}
. . .
$blender = new appliance;
A class definition creates several characteristics and functions pertinent to a data structure, in this case a data structure named appliance. So far, the appliance isn’t very functional. There is only one characteristic: power. This characteristic can be modified by using the method set_power. Remember, however, that a class definition is a template and cannot itself be manipulated. Instead, objects are created based on this template. This is accomplished via the new keyword. Therefore, in the preceding listing an object of class appliance named blender is created. The blender power can then be set by making use of the method set_power:
$blender->set_power("on");, “Object-Oriented PHP,” introduces PHP’s OOP implementation in further
detail.
Boolean, or True/False, Values
The boolean data type is essentially capable of representing only two data types: true and false. Boolean values can be determined in two ways: as a comparison evaluation or from a variable value. Both are rather straightforward. Comparisons can take place in many forms. Evaluation typically takes place by use of a double equal sign and an if conditional. Here is an example:
if ($sum == 40) :
. . .
This could evaluate to only either true or false. Either $sum equals 40, or it does not. If $sum does equal 40, then the expression evaluates to true. Otherwise, the result is false. Boolean values can also be determined via explicitly setting a variable to a true or false value. Here is an example:
$flag = TRUE;
if ($flag == TRUE) :
print "The flag is true!";
else :
print "The flag is false!";
endif;
If the variable $flag has been set to true, then print the appropriate statement; Otherwise, print an alternative statement. An alternative way to represent true and false is by using the values 1 and 0, respectively. Therefore, the previous example can be restated as follows:
$flag = 1;
if ($flag == TRUE) :
print "The flag is true!";
else :
print "The flag is false !";
endif;
Yet another alternative way to represent the above example follows:
$flag = TRUE;
// this implicitly asks "if ($flag == TRUE)"
if ($flag) :
print "The flag is true!";
else :
print "The flag is false!";
endif;
No comments:
Post a Comment