Thursday, 24 July 2008

PHP - Object-Oriented Programming (OOP) | Print |
 

Probably most of the backward-compatibility breaking changes in PHP 5 are in terms of its OOP support. Chapter 14, "Object-Oriented Programming in PHP," brought you up to date with the new features; here is a short recap about which of them might break old scripts.

The biggest change is also the one that could create problems with old code: passing objects. In PHP 4, this was done using values by default: An object was copied and this copy was then passed to a method. In PHP 5, this fundamentally changed. Now objects are passed by reference by default. That means that changing the object within the method it was passed to changes the original object. When the PHP core developers toured conferences with PHP 5 talks, this point created the most questions. Listing C.1 shows an example:

Listing C.1. Passing Objects
<?php

class Programmer {

var $name;



function Programmer($s) {

$this->name = $s;

}

}



function GeekMode($p) {

$p->name .= " aka Coogle";

}



$p = new Programmer("John Coggeshall");

echo("<p>They call him " . $p->name . "</p>");

GeekMode($p);

echo("<p>They also call him " . $p->name . "</p>");

?>


In PHP 4, "They call him John Coggeshall" is printed out twice. However in PHP 5, the second time it says, "They call him John Coggeshall aka Coogle". This is because the Programmer object is passed to the function GeekMode() by reference. Thus, changes to this object are also visible within the main routine.

This is also important when cloning objects. The command $object2 = $object1 creates a reference to $object1 in $object2. If you want to create a copy that you can modify independently, use the new clone command:

$object2 = clone $object1;


Other OOP changes seldom appear in current applications, but are nevertheless worth mentioning:

  • Object casting using (int)$object, a nifty trick in PHP 4 to find out whether an object has properties set, creates a warning in PHP 5 (and always the same result, 1).

  • In PHP 5, comparing objects with == returns true only if the references point to the identical object.

The recommended way to migrate an existing application is to rewrite the OOP code in your PHP scripts to cooperate with the new rules. For instance, most PEAR modules have done so shortly after the first betas of PHP 5, to allow usage both with PHP 4 and PHP 5. However under certain circumstances, a workaround might be of good use. Using the php.ini directive zend.ze1_compatibility_mode, you can make PHP 5 behave like PHP 4 in terms of object handling. This directive can also be set using ini_set(), if you do not have access to your php.ini file (for example, on shared hosts) or want to use this behavior only on certain pages:

ini_set(zend.ze1_compatibility_mode, "On");


 NOTE

Another change with no new PHP 5 equivalent is that within classes, no direct assignment to $this is allowed any longer. However, in almost all cases, there is no need to do so anyway.

 

Read More PHP:PHP - Functions | PHP - Including Other Files | PHP - Comments Tutorial | PHP - Example Whitespace | PHP - Variables of PHP | PHP - Basics of PHP | PHP Abnormal Script | PHP - Output Control | PHP - Common Style Mistakes

Link to this article:"Object-Oriented Programming (OOP)"

 

 

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 600

No comment posted

Search
Read This
The Importance of Google

The Importance of Google So why a special guide just on Google? Aren’t there hundreds of search...
Read more....

Using Public Keys in PHP

PHP includes support for the widely used and maintained OpenSSL library, which does nearly all the...
Read more....

All mIRC Commands

/ Recalls the previous command entered in the current window. /! Recalls the last command typed in...
Read more....

How to Select A Hard Drive For Your PC

Deciding on the appropriate hard drive is a personal decision made according to an ...
Read more....

Last Post
Live Traffic
Statistic


Excess Article
Last Comment

Home | Css Generator | Exchange Links | Computer | Ebook | English Section | Indo Section | Tips | Website | Sitemap
© 2007-2008 Online Science www.g-excess.com

Page Loading Time: 0.000014 seconds.