Docbookified README_UPGRADE_51.php

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@236188 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Hannes Magnusson 2007-05-23 18:44:28 +00:00
parent f8bbb073f5
commit f036018ad0

878
appendices/migration51.xml Normal file
View file

@ -0,0 +1,878 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<appendix id="migration51">
<title>Migrating from PHP 5.0.x to PHP 5.1.x</title>
<section id="migration51.changes">
<title>Key PHP 5.1.x features</title>
<para>
Some of the key features of PHP 5.1.x include:
</para>
<itemizedlist>
<listitem>
<para>
A complete rewrite of date handling code, with improved timezone support.
</para>
</listitem>
<listitem>
<para>
Significant performance improvements compared to PHP 5.0.X.
</para>
</listitem>
<listitem>
<para>
PDO extension is now enabled by default.
</para>
</listitem>
<listitem>
<para>
Over 30 new functions in various extensions and built-in functionality.
</para>
</listitem>
<listitem>
<para>
Over 400 various bug fixes.
</para>
</listitem>
</itemizedlist>
</section>
<section id="migration51.references">
<title>Changes in reference handling</title>
<itemizedlist>
<listitem>
<para>
<link linkend="migration51.references-overview">Overview</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.references-fails">Code that worked under PHP
4.3.x, but now fails</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.references-error">Code that worked under PHP
4.3.x, but now throws an error</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.references-works">Code that failed under PHP
4.3.x, but now works</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.references-didnotwork">Code that
<literal>should have worked</literal> under PHP 5.0.x</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.references-warnings">Warnings that came and
went</link>
</para>
</listitem>
</itemizedlist>
<section id="migration51.references-overview">
<title>Overview</title>
<para>
From 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.
</para>
<para>
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:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = "123";
function return_value() {
global $foo;
return $foo;
}
$bar = &return_value();
?>
]]>
</programlisting>
</informalexample>
<para>
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.
</para>
<para>
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
E_NOTICE in PHP 4.4.0 and up, and E_STRICT in PHP 5.0.4 and up.
</para>
<para>
Code that could potentially produce memory corruption can no longer do
so. However, some legacy code might work differently as a result.
</para>
</section>
<section id="migration51.references-fails">
<title>Code that worked under PHP 4.3, but now fails</title>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function func(&$arraykey) {
return $arraykey; // function returns by value!
}
$array = array('a', 'b', 'c');
foreach (array_keys($array) as $key) {
$y = &func($array[$key]);
$z[] =& $y;
}
var_dump($z);
?>
<]]>
</programlisting>
<para>
Running the above script under any version of PHP that pre-dates the
reference fix would produce this output:
</para>
<screen>
<![CDATA[
array(3) {
[0]=>
&string(1) "a"
[1]=>
&string(1) "b"
[2]=>
&string(1) "c"
}
]]>
</screen>
<para>
Following the reference fix, the same code would result in:
</para>
<screen>
<![CDATA[
array(3) {
[0]=>
&string(1) "c"
[1]=>
&string(1) "c"
[2]=>
&string(1) "c"
}
]]>
</screen>
</informalexample>
<para>
This is because, following the changes, func() assigns by value. The value
of $y is re-assigned, and reference-binding is preserved from $z. Prior to
the fix, the value was assigned by reference, leading $y to be re-bound on
each assignment. The attempt to bind to a temporary value by reference was
the cause of the memory corruption.
</para>
<para>
Such code can be made to work identically in both the pre-fix and the
post-fix PHP versions. The signature of func() can be altered to return by
reference, or the reference assignment can be removed from the result of
func().
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function func() {
return 'function return';
}
$x = 'original value';
$y =& $x;
$y = &func();
echo $x;
?>
]]>
</programlisting>
</informalexample>
<para>
In PHP 4.3 $x would be 'original value', whereas after the changes it
would be 'function return' - remember that where the function does not
return by reference, the reference assignment is converted to a regular
assignment. Again, this can be brought to a common base, either by forcing
func() to return by reference or by eliminating the by-reference
assignment.
</para>
</section>
<section id="migration51.references-error">
<title>Code that worked under PHP 4.3.x, but now throws an error</title>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class Foo {
function getThis() {
return $this;
}
function destroyThis() {
$baz =& $this->getThis();
}
}
$bar = new Foo();
$bar->destroyThis();
var_dump($bar);
?>
]]>
</programlisting>
</informalexample>
<para>
In PHP 5.0.3, $bar evaluated to NULL instead of returning an object. That
happened because getThis() returns by value, but the value here is
assigned by reference. Although it now works in the expected way, this is
actually invalid code which will throw an E_NOTICE under PHP 4.4 or an
E_STRICT under PHP 5.0.4 and up.
</para>
</section>
<section id="migration51.references-works">
<title>Code that failed under PHP 4.3.x, but now works</title>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function &f() {
$x = "foo";
var_dump($x);
print "$x\n";
return($a);
}
for ($i = 0; $i < 3; $i++) {
$h = &f();
}
?>
]]>
</programlisting>
<para>
In PHP 4.3 the third call to var_dump produces NULL, due to the memory
corruption caused by returning an uninitialized value by reference. This
is valid code in PHP 5.0.4 and up, but threw errors in earlier releases
of PHP.
</para>
</informalexample>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$arr = array('a1' => array('alfa' => 'ok'));
$arr =& $arr['a1'];
echo '-'.$arr['alfa']."-\n";
?>
]]>
</programlisting>
<para>
Until PHP 5.0.5, it wasn't possible to assign an array element by
reference in this way. It now is.
</para>
</informalexample>
</section>
<section id="migration51.references-didnotwork">
<title>Code that <literal>should have worked</literal> under PHP 5.0.x</title>
<para>
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 self:: now works in the general case
but throws an E_STRICT warning, and although your mileage may vary when
assigning by reference to an overloaded object, you will still see an
E_ERROR when you try it, even where the assignment itself appears to work.
</para>
</section>
<section id="migration51.references-warnings">
<title>Warnings that came and went</title>
<para>
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 E_NOTICE or E_STRICT under
the intervening PHP releases.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function & foo() {
$var = 'ok';
return $var;
}
function & bar() {
return foo();
}
$a =& bar();
echo "$a\n";
?>
]]>
</programlisting>
</informalexample>
</section>
</section><!-- end of migration51.references -->
<section id="migration51.reading">
<title>Reading []</title>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class XmlTest {
function test_ref(&$test) {
$test = "ok";
}
function test($test) { }
function run() {
$ar = array();
$this->test_ref($ar[]);
var_dump($ar);
$this->test($ar[]);
}
}
$o = new XmlTest();
$o->run();
?>
]]>
</programlisting>
</informalexample>
<para>
This should always have thrown a fatal E_ERROR, because [] cannot be used
for reading in PHP. It is invalid code in PHP 4.4.2 and PHP 5.0.5 upward.
</para>
</section>
<section id="migration51.integer-parameters">
<title>Integer values in function parameters</title>
<para>
With the advent of PHP 5.0.x, a new parameter parsing API was introduced
which is used by a large number of PHP functions. In all versions of PHP
between 5.0.x and 5.1.x, the handling of integer values was very strict and
would reject non-well formed numeric values when a PHP function expected an
integer. These checks have now been relaxed to support non-well formed
numeric strings such as " 123" and "123 ", and will no longer fail as they
did under PHP 5.0.x. However, to promote code safety and input validation,
PHP functions will now emit an E_NOTICE when such strings are passed as
integers.
</para>
</section>
<section id="migration51.oop">
<title>Class and object changes</title>
<itemizedlist>
<listitem>
<para>
<link linkend="migration51.oop-functions">instanceof, is_a(),
is_subclass_of() and catch</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.oop-methods">Abstract private methods</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.oop-modifiers">Access modifiers in
interfaces</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.oop-inheritance">Changes in inheritance
rules</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.oop-constants">Class constants</link>
</para>
</listitem>
</itemizedlist>
<section id="migration51.oop-functions">
<title>instanceof, is_a(), is_subclass_of() and catch</title>
<para>
In PHP 5.0, is_a() was deprecated and replaced by the "instanceof"
operator. There were some issues with the initial implementation of
"instanceof", which relied on __autoload() to search for missing classes.
If the class was not present, "instanceof" would throw a fatal E_ERROR due
to the failure of __autoload() to discover that class. The same behaviour
occurred in the "catch" operator and the is_subclass_of() function, for
the same reason.
</para>
<para>
None of these functions or operators call __autoload() in PHP 5.1.x, and the
class_exists() workarounds used in code written for PHP 5.0.x, while not
problematic in any way, are no longer necessary.
</para>
</section>
<section id="migration51.oop-methods">
<title>Abstract private methods</title>
<para>
Abstract private methods were supported between PHP 5.0.0 and PHP 5.0.4,
but were then disallowed on the grounds that the behaviours of 'private'
and 'abstract' are mutually exclusive.
</para>
</section>
<section id="migration51.oop-modifiers">
<title>Access modifiers in interfaces</title>
<para>
Under PHP 5.0, function declarations in interfaces were treated in exactly
the same way as function declarations in classes. This has not been the
case since October 2004, at which point only the 'public' access modifier
was allowed in interface function declarations. Since April 2005 - which
pre-dates the PHP 5.0b1 release - the 'static' modifier has also been
allowed. However, the 'protected' and 'private' modifiers will now throw
an E_ERROR, as will 'abstract'. Note that this change should not affect
your existing code, as none of these modifiers makes sense in the context
of interfaces anyway.
</para>
</section>
<section id="migration51.oop-inheritance">
<title>Changes in inheritance rules</title>
<para>
Under PHP 5.0, it was possible to have a function declaration in a derived
class that did not match the declaration of the same function in the base
class, e.g.
</para>
<informalexample>
<para>
This code will cause an E_STRICT error to be emitted under PHP 5.1.x.
</para>
<programlisting role="php">
<![CDATA[
<?php
class Base {
function &return_by_ref() {
$r = 1;
return $r;
}
}
class Derived extends Base {
function return_by_ref() {
return 1;
}
}
?>
]]>
</programlisting>
</informalexample>
</section>
<section id="migration51.oop-constants">
<title>Class constants</title>
<para>
Under PHP 5.1.x, redefinition of a class constant will throw a fatal
E_ERROR.
</para>
<informalexample>
<para>
Under PHP 5.1.x, redefinition of a class constant will throw a fatal
E_ERROR.
</para>
<programlisting role="php">
<![CDATA[
<?php
class test {
const foobar = 'foo';
const foobar = 'bar';
}
?>
]]>
</programlisting>
</informalexample>
</section>
</section> <!-- end of migration51.oop -->
<section id="migration51.extensions">
<title>Extensions</title>
<itemizedlist>
<listitem>
<para>
<link linkend="migration51.extensions-gone">Extensions that are gone
from the PHP core</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.extensions-constants">Class constants in new
PHP 5.1.x extensions</link>
</para>
</listitem>
</itemizedlist>
<section id="migration51.extensions-gone">
<title>Extensions that are gone from the PHP core</title>
<para>
One of the first things you're likely to notice when you download PHP 5.1.x
is that several of the older extensions have disappeared. Those extensions
that are still actively maintained are available in the PHP Extension
Community Library (PECL), at
<ulink url="&url.pecl;">&url.pecl;</ulink>. Windows binaries are
built regularly, and you can obtain the binaries for PECL extensions built
against PHP 5.1.x from
<ulink url="&url.pecl.win;">&url.pecl.win;</ulink>.
</para>
<table>
<title>Removed extensions</title>
<tgroup cols="2">
<thead>
<row>
<entry>Extension</entry>
<entry>Alternative/Status</entry>
</row>
</thead>
<tbody>
<row>
<entry><link linkend="ref.cpdf">ext/cpdf</link></entry>
<entry><link linkend="ref.pdf">pecl/pdflib</link></entry>
</row>
<row>
<entry><link linkend="ref.dbx">ext/dbx</link></entry>
<entry>pecl/dbx</entry>
</row>
<row>
<entry><link linkend="ref.dio">ext/dio</link></entry>
<entry>pecl/dio</entry>
</row>
<row>
<entry><link linkend="ref.fam">ext/fam</link></entry>
<entry>Not actively maintained</entry>
</row>
<row>
<entry><link linkend="ref.ingres">ext/ingres_ii</link></entry>
<entry>pecl/ingres</entry>
</row>
<row>
<entry><link linkend="ref.ircg">ext/ircg</link></entry>
<entry>Not actively maintained</entry>
</row>
<row>
<entry><link linkend="ref.mcve">ext/mcve</link></entry>
<entry>pecl/mcve</entry>
</row>
<row>
<entry><link linkend="ref.mnogosearch">ext/mnogosearch</link></entry>
<entry>Not actively maintained</entry>
</row>
<row>
<entry><link linkend="ref.oracle">ext/oracle</link></entry>
<entry>
<link linkend="ref.oci8">ext/oci8</link> or
<link linkend="ref.pdo-oci">ext/pdo_oci</link>
</entry>
</row>
<row>
<entry><link linkend="ref.ovrimos">ext/ovrimos</link></entry>
<entry>Not actively maintained</entry>
</row>
<row>
<entry><link linkend="ref.pfpro">ext/pfpro</link></entry>
<entry>Not actively maintained</entry>
</row>
<row>
<entry><link linkend="ref.w32api">ext/w32api</link></entry>
<entry><ulink url="&url.pecl.package;ffi">pecl/ffi</ulink></entry>
</row>
<row>
<entry><link linkend="ref.nis">ext/yp</link></entry>
<entry>Not actively maintained</entry>
</row>
<row>
<entry>ext/activescript</entry>
<entry>
<ulink url="url.pecl.win.ext;php5activescript.dll">pecl/activescript</ulink>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
Modules in PECL that are not actively maintained (i.e. have not been
supported for some time, have no active maintainer working on them
currently, and do not have any PECL package releases), are still available
in CVS at <ulink url="&url.php.cvs;pecl">&url.php.cvs;pecl</ulink>.
However, unreleased PHP modules are by their nature unsupported, and your
mileage may vary when attempting to install or use them.
</para>
</section>
<section id="migration51.extensions-constants">
<title>Class constants in new PHP 5.1.x extensions</title>
<para>
The Zend Engine 2.1 API allows extension developers to declare class
constants in object oriented extensions. New extensions written for PHP
5.1.x, including <link linkend="ref.spl">SPL</link>,
<link linkend="ref.pdo">PDO</link>,
<link linkend="ref.xmlreader">XMLReader</link> and
<link linkend="ref.datetime">date</link>, have their constants in
the format <literal>PDO::CLASS_CONSTANT</literal> rather than in the C
format <literal>PDO_CLASS_CONSTANT</literal> in order to minimise
pollution of the global namespace in PHP.
</para>
</section>
</section> <!-- end of migration51.extensions -->
<section id="migration51.datetime">
<title>Date/time support</title>
<para>
Date/time support has been fully rewritten in PHP 5.1.x, and no longer uses
the system settings to 'know' the timezone in operation. It will instead
utilize, in the following order:
</para>
<itemizedlist>
<listitem>
<para>
The timezone set using the <function>date_default_timezone_set</function>
function (if any)
</para>
</listitem>
<listitem>
<para>
The TZ environment variable (if non empty)
</para>
</listitem>
<listitem>
<para>
"magical" guess (if the operating system supports it)
</para>
</listitem>
<listitem>
<para>
If none of the above options succeeds, UTC
</para>
</listitem>
</itemizedlist>
<informalexample>
<para>
To ensure accuracy (and avoid an E_STRICT warning), you will need to define
your timezone in your php.ini using the following format:
</para>
<para>
date.timezone = Europe/London
</para>
</informalexample>
<para>
The supported timezones are listed, in this format, in the
<link linkend="timezones">timezones appendix</link>.
</para>
<para>
Also note that <function>strtotime</function> now returns &false; on failure, instead of -1.
</para>
</section>
<section id="migration51.databases">
<title>Changes in database support</title>
<itemizedlist>
<listitem>
<para>
<link linkend="migration51.databases-pdo">PDO overview</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.databases-mysql">Changes in MySQL support</link>
</para>
</listitem>
<listitem>
<para>
<link linkend="migration51.databases-sqlite">Changes in SQLite support</link>
</para>
</listitem>
</itemizedlist>
<section id="migration51.databases-pdo">
<title>PDO overview</title>
<para>
<link linkend="ref.pdo">PHP Data Objects (PDO)</link> were introduced as a
PECL extension under PHP 5.0, and became part of the core PHP distribution
in PHP 5.1.x. The PDO extension provides a consistent interface for database
access, and is used alongside database-specific PDO drivers. Each driver
may also have database-specific functions of its own, but basic data
access functionality such as issuing queries and fetching data is covered
by PDO functions, using the driver named in
<link linkend="function.PDO-construct">PDO::__construct()</link>.
</para>
<para>
Note that the PDO extension, and its drivers, are intended to be built as
shared extensions. This will enable straightforward driver upgrades from
PECL, without forcing you to rebuild all of PHP.
</para>
<para>
At the point of the PHP 5.1.x release, PDO is more than ready for widespread
testing and could be adopted in most situations. However, it is important
to understand that PDO and its drivers are comparatively young and may be
missing certain database-specific features; evaluate PDO carefully before
you use it in new projects.
</para>
<para>
Legacy code will generally rely on the pre-existing database extensions,
which are still maintained.
</para>
</section>
<section id="migration51.databases-mysql">
<title>Changes in MySQL support</title>
<para>
In PHP 4, MySQL 3 support was built-in. With the release of PHP 5.0 there
were two MySQL extensions, named 'mysql' and 'mysqli', which were designed
to support MySQL &lt; 4.1 and MySQL 4.1 and up, respectively. With the
introduction of PDO, which provides a very fast interface to all the
database APIs supported by PHP, the PDO_MYSQL driver can support any of
the current versions (MySQL 3, 4 or 5) in PHP code written for PDO,
depending on the MySQL library version used during compilation. The older
MySQL extensions remain in place for reasons of back compatibility, but
are not enabled by default.
</para>
</section>
<section id="migration51.databases-sqlite">
<title>Changes in SQLite support</title>
<para>
In PHP 5.0.x, SQLite 2 support was provided by the built-in sqlite
extension, which was also available as a PECL extension in PHP 4.3 and PHP
4.4. With the introduction of PDO, the sqlite extension doubles up to act
as a 'sqlite2' driver for PDO; it is due to this that the sqlite extension
in PHP 5.1.x has a dependency upon the PDO extension.
</para>
<para>
PHP 5.1.x ships with a number of alternative interfaces to sqlite:
</para>
<para>
The sqlite extension provides the "classic" sqlite procedural/OO API that
you may have used in prior versions of PHP. It also provides the PDO
'sqlite2' driver, which allows you to access legacy SQLite 2 databases
using the PDO API.
</para>
<para>
PDO_SQLITE provides the 'sqlite' version 3 driver. SQLite version 3 is
vastly superior to SQLite version 2, but the file formats of the two
versions are not compatible.
</para>
<para>
If your SQLite-based project is already written and working against
earlier PHP versions, then you can continue to use ext/sqlite without
problems, but will need to explicitly enable both PDO and sqlite. New
projects should use PDO and the 'sqlite' (version 3) driver, as this is
faster than SQLite 2, has improved locking concurrency, and supports both
prepared statements and binary columns natively.
</para>
<para>
You must enable PDO to use the SQLite extension. If you want to build the
PDO extension as a shared extension, then the SQLite extension must also
be built shared. The same holds true for any extension that provides a PDO
driver
</para>
</section>
</section> <!-- end of migration51.databases -->
<section id="migration51.errorcheck">
<title>Checking for <literal>E_STRICT</literal></title>
<informalexample>
<para>
If you only have a single script to check, you can pick up E_STRICT errors
using PHP's commandline lint facility:
</para>
<programlisting role="shell">
php -d error_reporting=4095 -l script_to_check.php
</programlisting>
<para>
For larger projects, the shell script below will achieve the same task:
</para>
<programlisting role="shell">
<![CDATA[
#!/bin/sh
directory=$1
shift
# These extensions are checked
extensions="php inc"
check_file ()
{
echo -ne "Doing PHP syntax check on $1 ..."
# Options:
ERRORS=`/www/php/bin/php -d display_errors=1 -d html_errors=0 -d error_prepend_string=" " -d error_append_string=" " -d error_reporting=4095 -l $1 | grep -v "No syntax errors detected"`
if test -z "$ERRORS"; then
echo -ne "OK."
else
echo -e "Errors found!\n$ERRORS"
fi
echo
}
# loop over remaining file args
for FILE in "$@" ; do
for ext in $extensions; do
if echo $FILE | grep "\.$ext$" > /dev/null; then
if test -f $FILE; then
check_file "$FILE"
fi
fi
done
done
]]>
</programlisting>
</informalexample>
</section>
</appendix>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax ft=docbkphp fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->