This is accomplished via the
new keyword. Therefore, in the last line of the previous listing, an object of class Appliance
named blender is created.
The blender object??™s power attribute can then be set by making use of the method
setPower():
$blender->setPower("on");
Improvements to PHP??™s object-oriented development model are a highlight of
PHP 5 and are further enhanced in PHP 6. Chapters 6 and 7 are devoted to thorough
coverage of PHP??™s object-oriented development model.
CHAPTER 3 ?– PHP BASICS 69
Converting Between Datatypes Using Type Casting
Converting values from one datatype to another is known as type casting. A variable can
be evaluated once as a different type by casting it to another. This is accomplished by
placing the intended type in front of the variable to be cast. A type can be cast by inserting
one of the operators shown in Table 3-2 in front of the variable.
Let??™s consider several examples. Suppose you??™d like to cast an integer as a double:
$score = (double) 13; // $score = 13.0
Type casting a double to an integer will result in the integer value being rounded
down, regardless of the decimal value. Here??™s an example:
$score = (int) 14.8; // $score = 14
What happens if you cast a string datatype to that of an integer? Let??™s find out:
$sentence = "This is a sentence";
echo (int) $sentence; // returns 0
In light of PHP??™s loosely typed design, it will simply return the integer value unmodified.
Pages:
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154