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!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

SanDisk MicroSDXC™ for Nintendo® Switch™

Learn more >

Breitling Superocean Heritage Chronographe 44

Learn more >

Toys for Boys

Family Friendly

Panasonic 4K UHD Blu-Ray Player and Full HD Recorder with Netflix - UBT1GL-K

Learn more >

Stocking Stuffer

Razer DeathAdder Expert Ergonomic Gaming Mouse

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?