php-doc-en/appendices/migration5.xml
2006-09-08 11:37:31 +00:00

936 lines
29 KiB
XML
Executable file

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.50 $ -->
<appendix id="migration5">
<title>Migrating from PHP 4 to PHP 5</title>
<section id='migration5.changes'>
<title>What has changed in PHP 5</title>
<para>
PHP 5 and the integrated Zend Engine 2 have greatly improved PHP's
performance and capabilities, but great care has been taken to break as
little existing code as possible. So migrating your code from PHP 4 to 5
should be very easy. Most existing PHP 4 code should be ready to run
without changes, but you should still know about the <link
linkend="migration5.incompatible">few differences</link> and
take care to test your code before switching versions in production
environments.
</para>
</section>
<section id="migration5.incompatible">
<title>Backward Incompatible Changes</title>
<para>
Although most existing PHP 4 code should work without changes, you should
pay attention to the following backward incompatible changes:
</para>
<itemizedlist>
<listitem>
<simpara>
There are some <link linkend="reserved.keywords">new reserved
keywords</link>.
</simpara>
</listitem>
<listitem>
<simpara>
<function>strrpos</function> and <function>strripos</function> now use
the entire string as a needle.
</simpara>
</listitem>
<listitem>
<simpara>
Illegal use of string offsets causes <constant>E_ERROR</constant> instead
of <constant>E_WARNING</constant>. An example illegal use is:
<literal>$str = 'abc'; unset($str[0]);</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_merge</function> was changed to accept only arrays. If a
non-array variable is passed, a <constant>E_WARNING</constant> will be
thrown for every such parameter. Be careful because your code may start
emitting <constant>E_WARNING</constant> out of the blue.
</simpara>
</listitem>
<listitem>
<simpara>
PATH_TRANSLATED server variable is no longer set implicitly under
Apache2 SAPI in contrast to the situation in PHP 4, where it is set to
the same value as the SCRIPT_FILENAME server variable when it is not
populated by Apache. This change was made to comply with the <ulink
url="&url.cgispecs;">CGI specification</ulink>. Please refer to <ulink
url="&url.php.bugs;23610">bug #23610</ulink> for further information,
and see also the <link linkend="reserved.variables.server">
$_SERVER['PATH_TRANSLATED']</link> description in the manual. This issue
also affects PHP versions &gt;= 4.3.2.
</simpara>
</listitem>
<listitem>
<simpara>
The <constant>T_ML_COMMENT</constant> constant is no longer defined by
the <link linkend="ref.tokenizer">Tokenizer</link> extension. If
error_reporting is set to <constant>E_ALL</constant>, PHP will
generate a notice. Although the <constant>T_ML_COMMENT</constant> was
never used at all, it was defined in PHP 4. In both PHP 4 and PHP 5
// and /* */ are resolved as the <constant>T_COMMENT</constant>
constant. However the PHPDoc style comments /** */, which starting PHP
5 are parsed by PHP, are recognized as <constant>T_DOC_COMMENT</constant>.
</simpara>
</listitem>
<listitem>
<simpara>
$_SERVER should be populated with argc and argv if <link
linkend="ini.variables-order">variables_order</link> includes "S". If
you have specifically configured your system to not create $_SERVER,
then of course it shouldn't be there. The change was to always make argc
and argv available in the CLI version regardless of the <link
linkend="ini.variables-order">variables_order</link> setting. As in,
the CLI version will now always populate the global $argc and $argv
variables.
</simpara>
</listitem>
<listitem>
<simpara>
An object with no properties is no longer considered "empty".
</simpara>
</listitem>
<listitem>
<simpara>
In some cases classes must be declared before use. It only happens if
some of the new features of PHP 5 (such as <link
linkend="language.oop5.interfaces">interfaces</link>) are used.
Otherwise the behaviour is the old.
</simpara>
</listitem>
<listitem>
<simpara>
<function>get_class</function>, <function>get_parent_class</function>
and <function>get_class_methods</function> now return the name of the
classes/methods as they were declared (case-sensitive) which may lead to
problems in older scripts that rely on the previous behaviour (the
class/method name was always returned lowercased). A possible solution
is to search for those functions in all your scripts and use
<function>strtolower</function>.
</simpara>
<simpara>
This case sensitivity change also applies to the
<link linkend="language.constants.predefined">magical predefined
constants</link> <constant>__CLASS__</constant>,
<constant>__METHOD__</constant>, and <constant>__FUNCTION__</constant>.
The values are returned exactly as they're declared (case-sensitive).
</simpara>
</listitem>
<listitem>
<simpara>
<function>ip2long</function> now returns &false; when an invalid IP
address is passed as argument to the function, and no longer
<literal>-1</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
If there are functions defined in the included file, they can be used in the
main file independent if they are before <function>return</function> or after.
If the file is included twice, PHP 5 issues fatal error because functions
were already declared, while PHP 4 doesn't complain about it.
It is recommended to use <function>include_once</function> instead of
checking if the file was already included and conditionally return inside
the included file.
</simpara>
</listitem>
<listitem>
<simpara>
<function>include_once</function> and <function>require_once</function>
first normalize the path of included file on Windows so that including
A.php and a.php include the file just once.
</simpara>
</listitem>
</itemizedlist>
<para>
<example>
<title><function>strrpos</function> and <function>strripos</function> now
use the entire string as a needle</title>
<programlisting role="php">
<![CDATA[
<?php
var_dump(strrpos('ABCDEF','DEF')); //int(3)
var_dump(strrpos('ABCDEF','DAF')); //bool(false)
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>An object with no properties is no longer considered "empty"</title>
<programlisting role="php">
<![CDATA[
<?php
class test { }
$t = new test();
var_dump(empty($t)); // echo bool(false)
if ($t) {
// Will be executed
}
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>In some cases classes must be declared before used</title>
<programlisting role="php">
<![CDATA[
<?php
//works with no errors:
$a = new a();
class a {
}
//throws an error:
$a = new b();
interface c{
}
class b implements c {
}
?>
]]>
</programlisting>
</example>
</para>
</section>
<section id="migration5.cli-cgi">
<title>CLI and CGI</title>
<para>
In PHP 5 there were some changes in CLI and CGI filenames. In PHP 5, the
CGI version was renamed to <filename>php-cgi.exe</filename> (previously
<filename>php.exe</filename>) and the CLI version now sits in the main
directory (previously <filename>cli/php.exe</filename>).
</para>
<para>
In PHP 5 it was also introduced a new mode:
<filename>php-win.exe</filename>. This is equal to the CLI version, except
that php-win doesn't output anything and thus provides no console (no "dos
box" appears on the screen). This behavior is similar to php-gtk.
</para>
<para>
In PHP 5, the CLI version will always populate the global
<varname>$argv</varname> and <varname>$argc</varname> variables regardless
of any &php.ini; directive setting. Even having
<link linkend="ini.register-argc-argv">register_argc_argv</link> set to
<literal>off</literal> will have no affect in CLI.
</para>
<para>
See also the <link linkend="features.commandline">commandline
reference</link>.
</para>
</section>
<section id="migration5.configuration">
<title>Migrating Configuration Files</title>
<para>
Since the ISAPI modules changed their names, from php4xxx to php5xxx, you
need to make some changes in the configuration files. There were also
changes in the CLI and CGI filenames. Please refer to the <link
linkend="migration5.cli-cgi">corresponding section</link> for more
information.
</para>
<para>
Migrate the Apache configuration is extremely easy. See the example below
to check the change you need to do:
<example>
<title>Migrating Apache configuration files for PHP 5</title>
<programlisting role="apache-conf">
<![CDATA[
# change this line:
LoadModule php4_module /php/sapi/php4apache2.dll
# with this one:
LoadModule php5_module /php/php5apache2.dll
]]>
</programlisting>
</example>
</para>
<para>
If your webserver is running PHP in CGI mode, you should note that the
CGI version has changed its name from <filename>php.exe</filename> to
<filename>php-cgi.exe</filename>.
In Apache, you should do something like this:
<example>
<title>Migrating Apache configuration files for PHP 5, CGI mode</title>
<programlisting role="apache-conf">
<![CDATA[
# change this line:
Action application/x-httpd-php "/php/php.exe"
# with this one:
Action application/x-httpd-php "/php/php-cgi.exe"
]]>
</programlisting>
</example>
</para>
<para>
In other webservers you need to change either the CGI or the ISAPI module
filenames.
</para>
</section>
<section id="migration5.functions">
<title>New Functions</title>
<para>
In PHP 5 there are some new functions. Here is the list of them:
</para>
<para><link linkend="ref.array">Arrays</link>:</para>
<itemizedlist>
<listitem>
<simpara>
<function>array_combine</function> - Creates an array by using one array
for keys and another for its values
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_diff_uassoc</function> - Computes the difference of
arrays with additional index check which is performed by a user supplied
callback function
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_udiff</function> - Computes the difference of arrays by
using a callback function for data comparison
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_udiff_assoc</function> - Computes the difference of
arrays with additional index check. The data is compared by using a
callback function
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_udiff_uassoc</function> - Computes the difference of
arrays with additional index check. The data is compared by using a
callback function. The index check is done by a callback function also
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_walk_recursive</function> - Apply a user function
recursively to every member of an array
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_uintersect_assoc</function> - Computes the intersection of
arrays with additional index check. The data is compared by using a
callback function
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_uintersect_uassoc</function> - Computes the intersection of
arrays with additional index check. Both the data and the indexes are
compared by using a callback functions
</simpara>
</listitem>
<listitem>
<simpara>
<function>array_uintersect</function> - Computes the intersection of arrays.
The data is compared by using a callback function
</simpara>
</listitem>
</itemizedlist>
<para><link linkend="ref.ibase">InterBase</link>:</para>
<itemizedlist>
<listitem>
<simpara>
<function>ibase_affected_rows</function> - Return the number of rows
that were affected by the previous query
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_backup</function> - Initiates a backup task in the
service manager and returns immediately
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_commit_ret</function> - Commit a transaction without
closing it
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_db_info</function> - Request statistics about a
database
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_drop_db</function> - Drops a database
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_errcode</function> - Return an error code
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_free_event_handler</function> - Cancels a registered
event handler
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_gen_id</function> - Increments the named generator and
returns its new value
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_maintain_db</function> - Execute a maintenance command
on the database server
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_name_result</function> - Assigns a name to a result set
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_num_params</function> - Return the number of parameters
in a prepared query
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_param_info</function> - Return information about a
parameter in a prepared query
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_restore</function> - Initiates a restore task in the
service manager and returns immediately
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_rollback_ret</function> - Rollback transaction and
retain the transaction context
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_server_info</function> - Request statistics about a
database
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_service_attach</function> - Connect to the service manager
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_service_detach</function> - Disconnect from the
service manager
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_set_event_handler</function> - Register a callback
function to be called when events are posted
</simpara>
</listitem>
<listitem>
<simpara>
<function>ibase_wait_event</function> - Wait for an event to be posted
by the database
</simpara>
</listitem>
</itemizedlist>
<para><link linkend="ref.iconv">iconv</link>:</para>
<itemizedlist>
<listitem>
<simpara>
<function>iconv_mime_decode</function> - Decodes a MIME header field
</simpara>
</listitem>
<listitem>
<simpara>
<function>iconv_mime_decode_headers</function> - Decodes multiple MIME
header fields at once
</simpara>
</listitem>
<listitem>
<simpara>
<function>iconv_mime_encode</function> - Composes a MIME header field
</simpara>
</listitem>
<listitem>
<simpara>
<function>iconv_strlen</function> - Returns the character count of
string
</simpara>
</listitem>
<listitem>
<simpara>
<function>iconv_strpos</function> - Finds position of first occurrence
of a needle within a haystack
</simpara>
</listitem>
<listitem>
<simpara>
<function>iconv_strrpos</function> - Finds the last occurrence of a
needle within a haystack
</simpara>
</listitem>
<listitem>
<simpara>
<function>iconv_substr</function> - Cut out part of a string
</simpara>
</listitem>
</itemizedlist>
<para><link linkend="ref.stream">Streams</link>:</para>
<itemizedlist>
<listitem>
<simpara>
<function>stream_copy_to_stream</function> - Copies data from one stream
to another
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_get_line</function> - Gets line from stream resource up
to a given delimiter
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_socket_accept</function> - Accept a connection on a
socket created by <function>stream_socket_server</function>
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_socket_client</function> - Open Internet or Unix domain
socket connection
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_socket_get_name</function> - Retrieve the name of the
local or remote sockets
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_socket_recvfrom</function> - Receives data from a
socket, connected or not
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_socket_sendto</function> - Sends a message to a socket,
whether it is connected or not
</simpara>
</listitem>
<listitem>
<simpara>
<function>stream_socket_server</function> - Create an Internet or Unix
domain server socket
</simpara>
</listitem>
</itemizedlist>
<para><link linkend="ref.datetime">Date and time related</link>:</para>
<itemizedlist>
<listitem>
<simpara>
<function>idate</function> - Format a local time/date as integer
</simpara>
</listitem>
<listitem>
<simpara>
<function>date_sunset</function> - Time of sunset for a given day and
location
</simpara>
</listitem>
<listitem>
<simpara>
<function>date_sunrise</function> - Time of sunrise for a given day and
location
</simpara>
</listitem>
<listitem>
<simpara>
<function>time_nanosleep</function> - Delay for a number of seconds and
nanoseconds
</simpara>
</listitem>
</itemizedlist>
<para><link linkend="ref.strings">Strings</link>:</para>
<itemizedlist>
<listitem>
<simpara>
<function>str_split</function> - Convert a string to an array
</simpara>
</listitem>
<listitem>
<simpara>
<function>strpbrk</function> - Search a string for any of a set of
characters
</simpara>
</listitem>
<listitem>
<simpara>
<function>substr_compare</function> - Binary safe optionally case
insensitive comparison of two strings from an offset, up to length
characters
</simpara>
</listitem>
</itemizedlist>
<para>Other:</para>
<itemizedlist>
<listitem>
<simpara>
<function>convert_uudecode</function> - decode a uuencoded string
</simpara>
</listitem>
<listitem>
<simpara>
<function>convert_uuencode</function> - uuencode a string
</simpara>
</listitem>
<listitem>
<simpara>
<function>curl_copy_handle</function> - Copy a cURL handle
along with all of its preferences
</simpara>
</listitem>
<listitem>
<simpara>
<function>dba_key_split</function> - Splits a key in string
representation into array representation
</simpara>
</listitem>
<listitem>
<simpara>
<function>dbase_get_header_info</function> - Get the header info of a
dBase database
</simpara>
</listitem>
<listitem>
<simpara>
<function>dbx_fetch_row</function> - Fetches rows from a query-result
that had the DBX_RESULT_UNBUFFERED flag set
</simpara>
</listitem>
<listitem>
<simpara>
<function>fbsql_set_password</function> - Change the password for a
given user
</simpara>
</listitem>
<listitem>
<simpara>
<function>file_put_contents</function> - Write a string to a file
</simpara>
</listitem>
<listitem>
<simpara>
<function>ftp_alloc</function> - Allocates space for a file to be
uploaded
</simpara>
</listitem>
<listitem>
<simpara>
<function>get_declared_interfaces</function> - Returns an array of all
declared interfaces
</simpara>
</listitem>
<listitem>
<simpara>
<function>get_headers</function> - Fetches all the headers sent by the
server in response to a HTTP request
</simpara>
</listitem>
<listitem>
<simpara>
<function>headers_list</function> - Returns a list of response headers
sent (or ready to send)
</simpara>
</listitem>
<listitem>
<simpara>
<function>http_build_query</function> - Generate URL-encoded query string
</simpara>
</listitem>
<listitem>
<simpara>
<function>image_type_to_extension</function> - Get file extension for
image-type returned by <function>getimagesize</function>,
<function>exif_read_data</function>,
<function>exif_thumbnail</function>,
<function>exif_imagetype</function>
</simpara>
</listitem>
<listitem>
<simpara>
<function>imagefilter</function> - Applies a filter to an image using
custom arguments
</simpara>
</listitem>
<listitem>
<simpara>
<function>imap_getacl</function> - Gets the ACL for a given mailbox
</simpara>
</listitem>
<listitem>
<simpara>
<function>ldap_sasl_bind</function> - Bind to LDAP directory using SASL
</simpara>
</listitem>
<listitem>
<simpara>
<function>mb_list_encodings</function> - Returns an array of all
supported encodings
</simpara>
</listitem>
<listitem>
<simpara>
<function>pcntl_getpriority</function> - Get the priority of any
process
</simpara>
</listitem>
<listitem>
<simpara>
<function>pcntl_wait</function> - Waits on or returns the status of a
forked child as defined by the waitpid() system call
</simpara>
</listitem>
<listitem>
<simpara>
<function>pg_version</function> - Returns an array with client, protocol
and server version (when available)
</simpara>
</listitem>
<listitem>
<simpara>
<function>php_check_syntax</function> - Check the syntax of the
specified file
</simpara>
</listitem>
<listitem>
<simpara>
<function>php_strip_whitespace</function> - Return source with stripped
comments and whitespace
</simpara>
</listitem>
<listitem>
<simpara>
<function>proc_nice</function> - Change the priority of the current
process
</simpara>
</listitem>
<listitem>
<simpara>
<function>pspell_config_data_dir</function> - Change location of
language data files
</simpara>
</listitem>
<listitem>
<simpara>
<function>pspell_config_dict_dir</function> - Change location of the
main word list
</simpara>
</listitem>
<listitem>
<simpara>
<function>setrawcookie</function> - Send a cookie without URL-encoding
the value
</simpara>
</listitem>
<listitem>
<simpara>
<function>scandir</function> - List files and directories inside the
specified path
</simpara>
</listitem>
<listitem>
<simpara>
<function>snmp_read_mib</function> - Reads and parses a MIB file into
the active MIB tree
</simpara>
</listitem>
<listitem>
<simpara>
<function>sqlite_fetch_column_types</function> - Return an array of
column types from a particular table
</simpara>
</listitem>
</itemizedlist>
<note>
<para>
The <link linkend="ref.tidy">Tidy</link> extension has also changed its
API completely.
</para>
</note>
</section>
<section id="migration5.newconf">
<title>New Directives</title>
<para>
There were some new &php.ini; directives introduced in PHP 5. Here is a
list of them:
</para>
<itemizedlist>
<listitem>
<simpara>
mail.force_extra_parameters - Force the addition of the specified
parameters to be passed as extra parameters to the sendmail binary.
These parameters will always replace the value of the 5th parameter to
<function>mail</function>, even in safe mode
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="ini.register-long-arrays">register_long_arrays</link> -
allow/disallow PHP to register the deprecated long $HTTP_*_VARS
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="ini.session.hash-function">session.hash_function</link> -
select a hash function (MD5 or SHA-1)
</simpara>
</listitem>
<listitem>
<simpara>
<link
linkend="ini.session.hash-bits-per-character">session.hash_bits_per_character</link>
- define how many bits are stored in each character when converting the
binary hash data to something readable (from 4 to 6)
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="ini.zend.ze1-compatibility-mode">zend.ze1_compatibility_mode</link>
- Enable compatibility mode with Zend Engine 1 (PHP 4)
</simpara>
</listitem>
</itemizedlist>
</section>
<section id="migration5.databases">
<title>Databases</title>
<para>
There were some changes in PHP 5 regarding databases (MySQL and SQLite).
</para>
<para>
In PHP 5 the MySQL client libraries are not bundled, because of license
problems and some others. For more information, read the <link
linkend="faq.databases.mysql.php5">FAQ entry</link>.
</para>
<para>
There is also a new extension, <link linkend="ref.mysqli">MySQLi (Improved
MySQL)</link>, which is designed to work with MySQL 4.1 and above.
</para>
<para>
Since PHP 5, the <link linkend="ref.sqlite">SQLite</link> extension is
built-in PHP. SQLite is an embeddable SQL database engine and is not a
client library used to connect to a big database server (like MySQL or
PostgreSQL). The SQLite library reads and writes directly to and from the
database files on disk.
</para>
</section>
<section id='migration5.oop'>
<title>New Object Model</title>
<para>
In PHP 5 there is a new Object Model. PHP's handling of objects has been
completely rewritten, allowing for better performance and more features.
In previous versions of PHP, objects were handled like primitive types
(for instance integers and strings). The drawback of this method was that
semantically the whole object was copied when a variable was assigned, or
passed as a parameter to a method. In the new approach, objects are
referenced by handle, and not by value (one can think of a handle as an
object's identifier).
</para>
<para>
Many PHP programmers aren't even aware of the copying quirks of the old
object model and, therefore, the majority of PHP applications will work
out of the box, or with very few modifications.
</para>
<para>
The new Object Model is documented at the <link
linkend="language.oop5">Language Reference</link>.
</para>
<para>
See also the <link linkend="ini.zend.ze1-compatibility-mode">
zend.ze1_compatibility_mode</link> directive for compatability with
PHP 4.
</para>
</section>
<section id='migrating5.errorrep'>
<title>Error Reporting</title>
<para>
As of PHP 5 new error reporting constant E_STRICT was introduced with
value 2048. It enables run-time PHP suggestions on your code
interoperability and forward compatibility, that will help you to keep
latest and greatest suggested method of coding. E.g. STRICT message will
warn you on using deprecated functions.
</para>
<note>
<simpara>
E_ALL does not include E_STRICT so it's not enabled by default
</simpara>
</note>
</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 fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->