Object Oriented PHP: Part 3

In case you haven't been reading, here is the series so far:

PHP has eight types of primitives: boolean, integer, double, string, array, resource, null and object. A class defines an object and an object is a primitive. Even though PHP won't always care what type of data a variable holds, there is an exception and there are times where it is smart to check, just for the sake of paranoia. Take for example a function that returns the string "false". When you test that string, you may not get the results you expect:


if ("false") {
// code executes as if the condition were true
}

To make sure you get exactly the results you want, you need to test the variable:


function testTheVariable() {
if (! is_bool($variable)) {
die ("Variable $variable is not of the type boolean.");
}
}

This method forces the client code to provide the proper variable type.

One situation where you must declare the variable type when you case it is in the case of an array. If you expect the data of a variable to return in array form, you'll want to declare it as such:


var $data = array();

Coming down the pipe next, class type hints and inheritance.

User login

Contacting Rob

Y! roberthenrylowe
AIM roberthenrylowe
ICQ 249971225
email rhlowe [at] gmail dot com

Networking Links