|
Changes in reference handlingFrom the PHP script writer's point of view, the change most likely to impact legacy code is in the way that references are handled in all PHP versions post-dating the PHP 4.4.0 release. Until and including PHP 4.3, it was possible to send, assign or return variables by reference that should really be returned by value, such as a constant, a temporary value (e.g. the result of an expression), or the result of a function that had itself been returned by value, as here: <?php Although this code would usually work as expected under PHP 4.3, in the general case the result is undefined. The Zend Engine could not act correctly on these values as references. This bug could and did lead to various hard-to-reproduce memory corruption problems, particularly where the code base was large.
In PHP 4.4.0, PHP 5.0.4 and all subsequent PHP releases, the Engine was
fixed to 'know' when the reference operation is being used on a value
that should not be referenced. The actual value is now used in such
cases, and a warning is emitted. The warning takes the form of an
Code that could potentially produce memory corruption can no longer do so. However, some legacy code might work differently as a result. <?php Running the above script under any version of PHP that pre-dates the reference fix would produce this output: array(3) { Following the reference fix, the same code would result in: array(3) {
This is because, following the changes,
Such code can be made to work identically in both the pre-fix and the
post-fix PHP versions. The signature of <?php
In PHP 4.3 <?php
In PHP 5.0.3, <?php
In PHP 4.3 the third call to var_dump() produces
<?php Until PHP 5.0.5, it wasn't possible to assign an array element by reference in this way. It now is.
There are a couple of instances of bugs reported under PHP 5.0 prior to
the reference fixes which now 'work'. However, in both cases errors are
thrown by PHP 5.1.x, because the code was invalid in the first place.
Returning values by reference using
Nested calls to functions returning by reference are valid code under both
PHP 4.3.x and PHP 5.1.x, but threw an unwarranted
<?php |