PHP 5: New object model

Thanks to input from people all over the Internet, PHP has been developed at a very fast pace. While this has contributed to PHP's ease of use, it has also led to the language being less than complete - especially when it comes to object orientation.

Python vs. PHP: Choosing your next project's language

PHP 5 has given developers a fresh slate to work on, and I'll be looking at its new features over the next few months.

Property visibility

Many object orientated languages allow developers to determine the visibility of object properties. These are called public, private and protected member variables. A public variable is one that can be accessed as a property of an object which is an instance of the given class. A private variable can only be accessed by methods of object itself. Protected members are like private ones, in that they cannot be accessed from outside the object. However, they can be accessed by classes extending the class in which the protected member is declared. For example:

01 <?
02 class test {
03 private $hidden = "some hidden data";
04 protected $prot = "some protected data";
05 public $visible = "visible data";
07 function _print() {
08 	 echo "test::_print() ".$this->hidden."\n";
09 	 echo "test::_print() ".$this->prot."\n";
10 	 echo "test::_print() ".$this->visible."\n";
11 }
12 }
13 class test2 extends test {
14 function _print() {
15 	 test::_print();
16 	 echo "test2::_print() ".$this->hidden."\n";
17 echo "test2::_print() ".$this->prot."\n";
18 echo "test2::_print() ".$this->visible."\n";
19 }
20 }
22 $obj1 = new test();
23 $obj1->_print();
24 $obj2 = new test2();
25 $obj2->_print();
26 ?>

On line 02, we declare a class 'test'. This class has three member variables: 'hidden', 'prot' and 'visible'. The function _print() on line 07 prints these variables. Notice that they are accessed as properties of the current object, $this, just as in PHP 4.

On line 13, we declare class 'test2'. This class extends the class 'test'. That is, the definitions in 'test2' are appended to those declared in 'test'.

On line 14, we declare _print(). As it is already declared in 'test', we are overloading it (replacing it) in this class. On line 14, within _print(), we call _print() in 'test'. This shows the visibility differences.

Finally, on line 22 we create an object from the class 'test' and call _print(). We do the same for 'test2'. The following text is output when this script is run against the PHP 5 interpreter:

1 test::_print() some hidden data
2 test::_print() some protected data
3 test::_print() visible data
4 test::_print() some hidden data
5 test::_print() some protected data
6 test::_print() visible data
7 test2::_print()
8 test2::_print() some protected data
9 test2::_print() visible data

Lines 1 through 6 are the result of calling test::_print(). Line 7 is test2::_print() trying to access the member 'hidden'. Since it is hidden, it is not declared in 'test2' and therefore defaults to an empty string. Lines 8 and 9 show that the protected and public members are visible to 'test2'.

By adding something like 'echo $obj2->hidden' at line 26, you will be able to see that the member is actually protected. The following error is generated:

Fatal error: Cannot access protected property test2::$prot in members.php on line 26

Members of a class can also be marked 'static'. A static member is one that can be accessed directly through the class structure. For example:

01 &lt?
02 class Stat {
03 	static $mydat = 10;
04 }
06 echo Stat::$mydat."\n";
07 ?>

Notice that we're able to access the variable $mydat before we have instantated the class as an object.

Similarly, member variables can now be marked 'const'. These are constants, much the same as those declared with define(). For example:

01 <?
02 class myConst {
03 	const myconst = "This is a constant";
04 }
05 echo myConst::myconst."\n";

Method visibility

The same mechanisms can also be applied to methods of a class. That is, methods can now be marked private, protected, public and static. Methods cannot be marked 'const' as only variables can be constants. For the other member types, however, the same semantics apply.

Join the newsletter!

Error: Please check your email address.

More about Hewlett-Packard AustraliaHP

Show Comments

Market Place