PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 3 ■ OBJECT BASICS


Any arguments supplied are passed to the constructor. So in my example I pass the title, the first
name, the main name, and the product price to the constructor. The constructor method uses the
pseudo-variable $this to assign values to each of the object’s properties.


■Note PHP 4 does not recognize the __construct() method as a constructor. If you are using PHP 4, you can


create a constructor by declaring a method with the same name as the class that contains it. So for a class called


ShopProduct, you would declare a constructor using a method named shopProduct().


PHP still honors this naming scheme, but unless you are writing for backward compatibility, it is better to use


__construct() when you name your constructor methods.


A ShopProduct object is now easier to instantiate and safer to use. Instantiation and setup are
completed in a single statement. Any code that uses a ShopProduct object can be reasonably sure that all
its properties are initialized.
This predictability is an important aspect of object-oriented programming. You should design your
classes so that users of objects can be sure of their features. By the same token, when you use an object,
you should be sure of its type. In the next section, I examine a mechanism that you can use to enforce
object types in method declarations.


Arguments and Types


Type determines the way that data can be managed in your scripts. You use the string type to display
character data, for example, and manipulate such data with string functions. Integers are used in
mathematical expressions; Booleans are used in test expressions, and so on. These categories are known
as primitive types. On a higher level, though, a class defines a type. A ShopProduct object, therefore,
belongs to the primitive type object, but it also belongs to the ShopProduct class type. In this section, I
will look at types of both kinds in relation to class methods.
Method and function definitions do not necessarily require that an argument should be of a
particular type. This is both a curse and a blessing. The fact that an argument can be of any type offers
you flexibility. You can build methods that respond intelligently to different data types, tailoring
functionality to changing circumstances. This flexibility can also cause ambiguity to creep into code
when a method body expects an argument to hold one type but gets another.


Primitive Types


PHP is a loosely typed language. This means that there is no necessity for a variable to be declared to
hold a particular data type. The variable $number could hold the value 2 and the string "two" within the
same scope. In strongly typed languages, such as C or Java, you must declare the type of a variable before
assigning a value to it, and, of course, the value must be of the specified type.
This does not mean that PHP has no concept of type. Every value that can be assigned to a variable
has a type. You can determine the type of a variable’s value using one of PHP’s type-checking functions.
Table 3-1 lists the primitive types recognized in PHP and their corresponding test functions. Each
function accepts a variable or value and returns true if this argument is of the relevant type.

Free download pdf