Some minor changes in functions break backward compatibility. There are
countless other additions to functions and additional functions, but these do
not affect compatibility between PHP 4 and PHP 5.
array_merge()
This function no longer accepts a non-array parameter as one of its argu-
ments. In PHP 4, it was perfectly valid to use scalar types, like an integer or
string (but not a variable representing “null”), as parameter. These types are
happily included as an element in the resulting array. PHP 5 no longer sup-
ports this. If you use a scalar type, PHP 5 issues an error of type E_WARNING and return an empty array. You can see this behavior by comparing the output of
this script from PHP 4 and PHP 5:
<?php
$array1 = array (1, 2, 3, 4);
$array2 = null;
$array3 = ‘non-array’;
$array4 = array (’a', ‘b’, ‘c’);
print_r(array_merge($array1, $array2, $array3, $array4));
?>
The output with PHP 4 is
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => non-array
[5] => a
[6] => b
[7] => c
)
The output with PHP 5 is
Warning: array_merge(): Argument #2 is not an array in /13-making
the-move/array_merge.php on line 7
Warning: array_merge(): Argument #3 is not an array in /13-making
the-move/array_merge.php on line 7
strrpos() and strripos()
strrpos() and strripos() search for the last occurrence of a string inside a
string in a respectively case-sensitive and case-insensitive way. In PHP 5, the
full $needle is searched for in the string, searching from the end rather than
the first character of this $needle string, as in PHP 4. The following example
shows this:
<?php
$str = “This is a short string.”;
var_dump(strrpos($str, “small”));
?>
In PHP 4, this returns position 16 (the index of the “s” of “string”):
int(16)
In PHP 5 this returns
bool(false)
It is possible that more functions broke compatibility between PHP 4 and
PHP 5, but they are either not known, a bug fix, or are too unimportant to be
noticed.
SUMMARY
This chapter highlights some changes in PHP 5 that affect scripts written and
working under PHP 4. A new object model and new OO features in PHP 5
mean that some OO scripts written for PHP 4 won’t run correctly with PHP 5.
If you cast an object to an int, the result is always 1, rather than 0 as in PHP
4. When you compare objects in PHP 5, true is returned only if the objects are
the same, with the same object handle. The PHP 5 behavior for these three
changes can be reverted to PHP 4 behavior by turning on Zend 1 compatibility
mode in the php.ini file. However, two other changes cannot be reverted. In
PHP 5, you can no longer assign an object to $this inside a class. In addition,
the get_class() function in PHP 5 returns the class name with its upper- and
lowercase characters preserved. As well as changes, some features are depre-
cated. A new error type–E_STRICT–warns you when you use deprecated fea-
tures, as long as you specify E_STRICT errors in the php.ini file. Although PHP
still allows the automatic creation of objects of class StdClass by assigning a
value to a property, you get an E_STRICT error when you do this. Also, the var
designation for properties is deprecated in favor of public. In addition, a new
constructor–construct()–is introduced with PHP 5. PHP 5 throws an
E_STRICT error when it encounters a function in an inherited class with a dif-
ferent signature than a function of the same name in the parent class.
As well as OO changes, a few other changes break backward compatibil-
ity. When setting up PHP on Windows, the names and locations some of the
files in the distribution have changed. For instance, the CGI binary is now
called php-cgi.exe rather than php.exe. The parser changed the way it tokenizes
comments. MySQL is no longer enabled by default and the client library is no
longer bundled, so you need to use an external library. The array_merge() func-
tion no longer accepts a non-array parameter, and strrpos() and strripos()
now use the full $needle to search for a substring in a string. There are many
other changes, including additional features for functions and new functions,
but most changes do not affect existing scripts that run with PHP 4.
Post a Comment