PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 3 ■ OBJECT BASICS

Table 3-1. Primitive Types and Checking Functions in PHP


Type Checking Function Type Description

is_bool() Boolean One of the two special values true or false

is_integer() Integer A whole number

is_double() Double A floating point number (a number with a decimal point)
is_string() String Character data

is_object() Object An object
is_array() Array An array

is_resource() Resource
A handle for identifying and working with external
resources such as databases or files
is_null() Null An unassigned value

Checking the type of a variable can be particularly important when you work with method and
function arguments.


Primitive Types Matter: An Example


You need to keep a close eye on type in your code. Here’s an example of one of the many type-related
problems that you could encounter.
Imagine that you are extracting configuration settings from an XML file. The XML
element tells your application whether it should attempt to resolve IP addresses to domain names, a
useful but relatively expensive process in terms of time. Here is some sample XML:



false

The string "false" is extracted by your application and passed as a flag to a method called
outputAddresses(), which displays IP address data. Here is outputAddresses():


class AddressManager {
private $addresses = array( "209.131.36.159", "74.125.19.106" );


function outputAddresses( $resolve ) {
foreach ( $this->addresses as $address ) {
print $address;
if ( $resolve ) {
print " (".gethostbyaddr( $address ).")";
}
print "\n";
}
}
}

Free download pdf