If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Threaded View

Differences between PHP 4 and PHP 5

Differences between PHP 4 & PHP 5

If you are planning on migrating from PHP 4 to PHP 5, here are the main changes you should be aware of.

PHP 5 Object Model
PHP 5 features a greatly improved object model which brings it a lot closer to a fully fledged object orientated programming language. If you have used the PHP 4 object model in any of your applications, here are the major differences between the two.

Assignments and Object Copying
By default, in PHP 4, creating an instance of or assigning a previously instantiated object to a new variable will cause a copy of the object to be created.

PHP Code:

/* PHP 4 */
$object = new MObject; // a copy of the new instance will be assigned to $object

$object2 = $object; // $object will be copied and then assigned to $object2

$object->property = 1; // changes made to $object are not reflected in $object2

As of PHP 5, to give the programmer more flexibility, references are used by default. To obtain a copy of an object in PHP 5, you need to clone it.

PHP Code:

/* php 5 */
$object = new MObject; // a reference will be assigned to $object

$object2 = $object; // assigns the $object reference to $object2

$object->property = 1; // changes made to $object are reflected in $object2