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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Essentials

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?