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";
06
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 }
21
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 }
05
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!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Father’s Day Gift Guide

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?