Prev | Current Page 255 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

This is because referring to
static fields using the means allowed for their ???regular??? siblings is not possible and will
result in a syntax error if attempted.
?– Note You can??™t use $this within a class to refer to a field declared as static.
CHAPTER 6 ?–  O BJECT-ORIENTED PHP 189
The instanceof Keyword
The instanceof keyword was introduced with PHP 5. With it you can determine whether
an object is an instance of a class, is a subclass of a class, or implements a particular
interface, and do something accordingly. For example, suppose you want to learn
whether an object called manager is derived from the class Employee:
$manager = new Employee();
...
if ($manager instanceof Employee) echo "Yes";
There are two points worth noting here. First, the class name is not surrounded by
any sort of delimiters (quotes). Including them will result in a syntax error. Second,
if this comparison fails, the script will abort execution. The instanceof keyword is
particularly useful when you??™re working with a number of objects simultaneously.
For example, you might be repeatedly calling a particular function but want to tweak
that function??™s behavior in accordance with a given type of object. You might use a
case statement and the instanceof keyword to manage behavior in this fashion.
Helper Functions
A number of functions are available to help the developer manage and use class
libraries.


Pages:
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267