[ Team LiB ] Previous Section Next Section

Limiting Access to Object Properties

PHP 4 provided no protection for object properties. Client code could get or set object properties at will. So what's wrong with that? Often, there is no problem having publicly accessible properties, although it is generally good practice to narrow access to your objects as much as possible. In Listing 9.5, we can see a condition in which we would definitely want to limit access to the $name property in our Item class.

Listing 9.5 A Class with Public Properties
 1: <?php
 2: class item {
 3:   var $name;
 4:   var $code;
 5:   var $productString;
 6:
 7:   function Item( $name="item", $code=0 ) {
 8:     $this->name = $name;
 9:     $this->code = $code;
10:    $this->setName( $name );
11: }
12:
13: function getProductString () {
14:   return $this->productString;
15:  }
16:
17:  function setName( $n ) {
18:    $this->name = $n;
19:    $this->productString = $this->name." ".$this->code;
20:  }
21:
22:  function getName () {
23:    return $this->name;
24:  }
25: }
26:
27: $item = new Item ("widget", 5442);
28: print $item->getProductString ();
29: // outputs "widget 5442"
30:
31: print "<br />";
32:
33: $item->name = "widget-upgrade";
34: print $item->getProductString ();
35: // outputs "widget 5442", not "widget-upgrade 5442"
36: ?>

We have made some changes to the Item class in Listing 9.5. The constructor now expects two arguments on line 7, $name and $code. In our example, then, a product has a human-readable name (such as widget) and a product code (such as 5442) used by a database. We have a new property, $productString, which is a combination of the item's $name and $code properties. Whenever client code uses setName() to change the name property on line 17, the method also updates $productString. So when we bypass the setName() method on line 33 and set the $name property manually, we break the object. The $productString property is no longer correct.

PHP 5 gives us a different way to declare our properties. In place of the var keyword, we could use one of three new keywords. They will be familiar to anyone moving from Java to PHP. We list PHP 5's new property declaration keywords in Table 9.1.

Table 9.1. PHP 5 Property Declaration Keywords

Privacy Level

Description

public

Accessible to all. Equivalent to var.

private

Available only to the containing class.

protected

Available only to the containing class and subclasses.

We can amend our properties in Listing 9.5 to make them private:


private $name;
private $code;
private $productString;

Now, our attempt to change the $name property of the Item object on line 34 would result in the following error message:



Fatal error: Cannot access private property Item::$name in /home/mz/htdocs/Listing 9.5.php on
graphics/ccc.gif line 33

Client coders are now forced to use the setName() method to change the $name property.

The private keyword is the most extreme mechanism for ensuring privacy. You might often want child classes to have access to a property. In these cases, you would use the protected keyword, which allows no access from client code but does allow access by classes derived from the current class. We will look at inheritance and privacy in the section "Inheritance."

    [ Team LiB ] Previous Section Next Section