[ Team LiB ] Previous Section Next Section

Object Methods

A method is a function defined within a class. Every object instantiated from the class has the method's functionality. Listing 9.1 adds a method to the Item class (line 5).

Listing 9.1 A Class with a Method
 1: <?php
 2:class Item {
 3:  var $name = "item";
 5:  function getName() {
 6:    return "item";
 7:  }
 8: }
10: $item = new Item ();
11: print $item->getName ();
12: // outputs "item"
13: ?>

As you can see, a method looks and behaves much like a normal function. A method is always defined within a class, however. You can call an object method using the -> operator. Importantly, methods have access to the class's member variables. In Listing 9.2, we return the string "item" when asked for the Item object's name. Clearly, this isn't good practice: the method's return value should be a copy of the $name property and not a string literal. You've already seen how to access a property from outside an object, but how does an object refer to itself? Find out in Listing 9.2.

Listing 9.2 Accessing a Property from Within a Method
 2:class Item {
 3:  var $name = "item";
 5:  function getName () {
 6:    return $this->name;
 7:  }
 8: }
10: $item = new Item ();
11: $item->name = "widget 5442";
12: print $item->getName ();
13: // outputs "widget 5442"
14: ?>

A class uses the special variable $this to refer to the currently instantiated object (line 6). You can think of it as a personal pronoun. Although you refer to an object by the handle you have assigned it to ($item, for example), an object must refer to itself by means of the $this variable. Combining the $this pseudovariable and ->, you can access any property or method in a class from within the class itself.

Imagine that you want to allow some objects to have different $name property values than others. You could do this by manually resetting the $name property as you did earlier, or you could create a method to do it for you, as shown in Listing 9.3 on line 10.

Listing 9.3 Changing the Value of a Property from Within a Method
 2: class Item {
 3:   var $name = "item";
 5:   function setName( $n ) {
 6:     $this->name = $n;
 7:   }
 9:   function getName() {
10:     return $this->name;
11:   }
12: }
14: $item = new Item();
15: $item->setName("widget 5442");
16: print $item->getName ();
17: // outputs "widget 5442"
18: ?>

The name property of the object begins as "item" (line 3), but after the object's setName() method is called on line 15, it is changed to "widget 5442". Notice how the object is capable of adjusting its own property. Notice also that you can pass arguments to the method in exactly the same way as you would to a normal function.

Object Constructors

In our previous example, we used a method, setName(), to amend the $name property. The initial value for the name property was hard-coded into the class:

var $name = "item";

If we expect the $name property to hold a different value for every instance of the Item class, we would do better to offer the client coder the chance to set the $name property when the object is initialized. We can use a special function called a constructor to set properties and perform any other preparatory work we require. A constructor is automatically called when the object is instantiated using the new keyword.

You can create constructors in two ways. Prior to PHP 5, the constructor was always a function that took the same name as the class that contained it. Listing 9.4 adds a traditional constructor to the Item class. This code remains valid in PHP 5.

Listing 9.4 A Class with a Constructor
 2: class Item {
 3:   var $name;
 5:   function Item( $name="item") {
 6:     $this->name = $name;
 7:   }
 9:   function setName( $n) {
10: $this->name = $n;
11: }
13: function getName () {
14: return $this->name;
15: }
16: }
18: $item = new Item("widget 5442");
19: print $item->getName ();
20: // outputs "widget 5442"
21: ?>

The Item() constructor method on line 5 is automatically called when we instantiate an Item object. We set up a default so that the string "item" is assigned to the parameter if we don't include an argument when we create our object.

PHP 5 introduces a new syntax for constructor methods. Instead of using the name of the class, you can use the special syntax __construct(). So we could convert line 5 of Listing 9.4 to use the new syntax by replacing Item() with ___construct():

function __construct( $name="listing") {
  // ..

This is not change for its own sake. We will encounter a good reason for using the new constructor syntax later in the chapter.

The ___construct() Method Works with PHP 5 Only


The __construct() method was introduced with PHP 5. The method name does not have special significance in PHP 4, and the method is not called automatically.

    [ Team LiB ] Previous Section Next Section