Dec 7

从PHP4移植到PHP5 不指定

bkkkd , 19:20 , 开发应用 , 评论(0) , 引用(0) , 阅读(4836) , Via 本站原创 | |
本来打算翻译一下有关在PHP.net上的有关从PHP4移植到PHP5的内容,没想到有人捷足先登了,多谢了。真是高兴最近大家对PHP的支持。这里我整理下来给你大家看看。

如果大家要看原文参考:

http://www.php.net/manual/en/migration5.php

从PHP4移植到PHP5

目录
PHP 5 中有哪些改变
向后不兼容的改变
CLI 和 CGI
移植配置文件
新函数
新指令
数据库
新对象模型
错误报告


PHP 5 中有哪些改变

PHP 5 和集成的 Zend Engine 2 代极大提高了 PHP 的性能和能力,而且投入极大努力尽可能避免破坏已有的代码的使用。因此将代码从 PHP 4 移植到 PHP 5 应该很容易。大多数已有的 PHP 4 代码应该不用改就能运行,但还是应该注意到几点区别并在实际运作环境下转换到新版本之前多做测试。

向后不兼容的改变

尽管大部分 PHP 4 的代码应该不用修改就能运行,还是应该留意以下向后不兼容的改变:


有了一些新关键字。

strrpos() 和 strripos() 如今使用整个字符串作为 needle。

非法使用字符串偏移量会导致 E_ERROR 而不是 E_WARNING。一个非法使用的例子:$str = 'abc'; unset($str[0]);.

array_merge() 被改成只接受数组。如果传递入非数组变量,对每个此类参数都会发出一条 E_WARNING 信息。要小心因为你的代码有可能疯狂发出 E_WARNING。

PATH_TRANSLATED 服务器变量在 Apache2 SAPI 中不再暗中设定,这和 PHP 4 中的情形相反,如果 Apache 没产生此值则其被设为和 SCRIPT_FILENAME 服务器变量一样的值。此修改是为了遵守 CGI 规范。更多信息见 bug #23610,并参考手册中 $_SERVER['PATH_TRANSLATED'] 的说明。此问题也影响到 PHP >= 4.3.2 的版本。

Tokenizer 扩展不再定义 T_ML_COMMENT 常量。如果把 error_reporting 设为 E_ALL,PHP 将产生一条消息。尽管 T_ML_COMMENT 从来都没用到过,还是在 PHP 4 中定义了。在 PHP 4 和 PHP 5 中 // 和 /* */ 都被解析为 T_COMMENT 常量。但是 PHPDoc 风格的注释 /** */,自 PHP 5 开始被 PHP 解析,被识别为 T_DOC_COMMENT。

如果 variables_order 包括“S”,$_SERVER 应该带有 argc 和 argv 被产生。如果用户特别配制系统不创建 $_SERVER,那此变量当然就不存在了。改变的地方是不管 variables_order 怎么设定,在 CLI 版本中 argc 和 argv 总是可用的。本来 CLI 版不是总会产生全局变量 $argc 和 $argv 的。

没有属性的对象不再被当成“empty”。

有些情况下类必须在使用前被定义。这仅在使用了一些 PHP 5 的新特性的时候发生。其它情况下行为都没变。

get_class(),get_parent_class() 和 get_class_methods() 如今返回的类/方法名和定义时的名字一致(区分大小写),对于依赖以前行为(类/方法名总是返回小写的)的老脚本可能产生问题。一个可能的解决方法是在脚本中搜索所有这些函数并使用 strtolower()。

区分大小写的改变也适用于魔术常量 __CLASS__,__METHOD__ 和 __FUNCTION__。其值都会严格按照定义时的名字返回(区分大小写)。

ip2long() 在传递入一个非法 IP 作为参数时返回 FALSE,不再是 -1。

如果在包含文件中定义有函数,则不管在 return() 之前还是之后都可以在主文件中调用。如果文件被包含两次,PHP 5 会发出致命错误,因为函数已经被定义,而 PHP 4 不管这个。因此推荐使用 include_once() 而不要去检查文件是否已被包含以及在包含文件中有条件返回。

include_once() 和 require_once() 在 Windows 下先将路径规格化,因此包含 A.php 和 a.php 只会把文件包含一次。

例子 B-1. strrpos() 和 strripos() 如今用整个字符串作为 needle

var_dump(strrpos('ABCDEF','DEF')); //int(3)

var_dump(strrpos('ABCDEF','DAF')); //bool(false)
?>



例子 B-2. 没有属性的对象不再被当成“empty”

class test { }
$t = new test();

var_dump(empty($t)); // echo bool(false)

if ($t) {
// Will be executed
}
?>



例子 B-3. 有些情况下类必须在使用之前定义


//works with no errors:
$a = new a();
class a {
}


//throws an error:
$a = new b();

interface c{
}
class b implements c {
}

?>


CLI 和 CGI

PHP 5 中对 CLI 和 CGI 文件名作了些改变。PHP 5 中,CGI 版本被改名为 php-cgi.exe(以前是 php.exe),现在主目录中的是 CLI 版本(之前是 cli/php.exe)。

PHP 5 中引进了一种新模式:php-win.exe。这和 CLI 版本相同,只除了 php-win 不输出任何内容,因此不会提供控制台(屏幕上不会闪过“dos 窗口”)。此行为类似 php-gtk。

PHP 5 中,CLI 版本总会产生全局变量 $argv 和 $argc 而不管 php.ini 是怎么设的。即使将 register_argc_argv 设为 off 也不影响 CLI。

参见命令行模式。

移植配置文件
由于 ISAPI 模块的名字改了,从 php4xxx 改为 php5xxx,因此需要对配置文件作些修改。CLI 和 CGI 文件名也改了。更多信息请查看相应章节。

移植 Apache 配置极其简单。照下面的例子来检查需要做的修改: 例子 B-4. 移植 Apache 配置文件到 PHP 5

# 将下面这行:
LoadModule php4_module /php/sapi/php4apache2.dll

# 改成这一行:
LoadModule php5_module /php/php5apache2.dll




如果 web 服务器是以 CGI 模式运行 PHP 的,应该注意 CGI 版本的名字从 php.exe 改为了 php-cgi.exe。在 Apache 中,应该照这样改: 例子 B-5. 移植 Apache 配置文件到 PHP 5,CGI 模式

# 将下面这行:
Action application/x-httpd-php "/php/php.exe"

# 改成这一行:
Action application/x-httpd-php "/php/php-cgi.exe"




其它的 web 服务器中,需要修改 CGI 或者 ISAPI 模块的名字。


新函数


PHP 5 有了些新函数。下面是列表:

Arrays:


array_combine() - 用一个数组作为键名,另一个数组作为值创建一个新数组

array_diff_uassoc() - 计算数组的差别,并用用户提供的回调函数作附加的索引检查

array_udiff() - 用回调函数比较数据来计算数组的差别

array_udiff_assoc() - 计算数组的差别并作附加的索引检查。用回调函数来比较数据

array_udiff_uassoc() - 计算数组的差别并作附加的索引检查。数据的比较和索引检查都用回调函数来完成

array_walk_recursive() - 对数组的每个成员递归使用用户函数

array_uintersect_assoc() - 计算数组的交集并作附加的索引检查。用回调函数来比较数据

array_uintersect_uassoc() - 计算数组的交集并作附加的索引检查。数据和索引都用回调函数来比较

array_uintersect() - 计算数组的交集。用回调函数来比较数据

InterBase:


ibase_affected_rows() - 返回前一个查询影响到的行的数目

ibase_backup() - 在服务管理器中发起一个后台任务并立即返回

ibase_commit_ret() - 提交一个事务但不关闭

ibase_db_info() - 请求有关数据库的统计信息

ibase_drop_db() - 删除一个数据库

ibase_errcode() - 返回一个错误代码

ibase_free_event_handler() - 取消一个已注册的事件句柄

ibase_gen_id() - 递增指定的发生器并返回其新值

ibase_maintain_db() - 在数据库服务器上执行一条维护命令

ibase_name_result() - 给结果集指定一个名字

ibase_num_params() - 返回一个准备好的查询的参数数目

ibase_param_info() - 返回一个准备好的查询的参数信息

ibase_restore() - 在服务管理器中发起一个还原任务并立即返回

ibase_rollback_ret() - 回卷一笔事务并保留事务上下文

ibase_server_info() - 请求有关数据库服务器的统计信息

ibase_service_attach() - 连接到服务管理器

ibase_service_detach() - 从服务管理器断开

ibase_set_event_handler() - 注册一个当事件发布时要调用的回调函数

ibase_wait_event() - 等待数据库发布一条事件

iconv:


iconv_mime_decode() - 解码 MIME 头信息字段

iconv_mime_decode_headers() - 一次解码多个 MIME 头信息字段

iconv_mime_encode() - 压缩 MIME 头信息字段

iconv_strlen() - 返回字符串中的字符计数

iconv_strpos() - 在堆栈中找到第一个出现的子串位置

iconv_strrpos() - 在堆栈中指定的范围内找到最后一个出现的子串位置

iconv_substr() - 从字符串中取出一部分

Streams:


stream_copy_to_stream() - Copies data from one stream to another

stream_get_line() - Gets line from stream resource up to a given delimiter

stream_socket_accept() - Accept a connection on a socket created by stream_socket_server()

stream_socket_client() - Open Internet or Unix domain socket connection

stream_socket_get_name() - Retrieve the name of the local or remote sockets

stream_socket_recvfrom() - Receives data from a socket, connected or not

stream_socket_sendto() - Sends a message to a socket, whether it is connected or not

stream_socket_server() - Create an Internet or Unix domain server socket

Date and time related:


idate() - Format a local time/date as integer

date_sunset() - Time of sunset for a given day and location

date_sunrise() - Time of sunrise for a given day and location

time_nanosleep() - Delay for a number of seconds and nano seconds

Strings:


str_split() - Convert a string to an array

strpbrk() - Search a string for any of a set of characters

substr_compare() - Binary safe optionally case insensitive comparison of two strings from an offset, up to length characters

Other:


convert_uudecode() - decode a uuencoded string

convert_uuencode() - uuencode a string

curl_copy_handle() - Copy a cURL handle along with all of it's preferences

dba_key_split() - Splits a key in string representation into array representation

dbase_get_header_info() - Get the header info of a dBase database

dbx_fetch_row() - Fetches rows from a query-result that had the DBX_RESULT_UNBUFFERED flag set

fbsql_set_password() - Change the password for a given user

file_put_contents() - Write a string to a file

ftp_alloc() - Allocates space for a file to be uploaded

get_declared_interfaces() - Returns an array of all declared interfaces

get_headers() - Fetches all the headers sent by the server in response to a HTTP request

headers_list() - Returns a list of response headers sent (or ready to send)

http_build_query() - Generate URL-encoded query string

image_type_to_extension() - Get file extension for image-type returned by getimagesize(), exif_read_data(), exif_thumbnail(), exif_imagetype()

imagefilter() - Applies Filter an image using a custom angle

imap_getacl() - Gets the ACL for a given mailbox

ldap_sasl_bind() - Bind to LDAP directory using SASL

mb_list_encodings() - Returns an array of all supported encodings

pcntl_getpriority() - Get the priority of any process

pcntl_wait() - Waits on or returns the status of a forked child as defined by the waitpid() system call

pg_version() - Returns an array with client, protocol and server version (when available)

php_check_syntax() - Check the syntax of the specified file

php_strip_whitespace() - Return source with stripped comments and whitespace

proc_nice() - Change the priority of the current process

pspell_config_data_dir() - Change location of language data files

pspell_config_dict_dir() - Change location of the main word list

setrawcookie() - Send a cookie with no url encoding of the value

snmp_read_mib() - Reads and parses a MIB file into the active MIB tree

sqlite_fetch_column_types() - Return an array of column types from a particular table

注: The Tidy extension has also changed its API completely.

新指令

PHP 5 在 php.ini 中引进了一些新指令。列表如下:


mail.force_extra_parameters - 强制指定的参数附加值作为额外的参数传递给 sendmail 库。这些参数总是会替换掉 mail() 的第 5 个参数,即使在安全模式下

register_long_arrays - 允许/禁止 PHP 注册已过时的 $HTTP_*_VARS 变量

session.hash_function - 选择一种散列函数(MD5 或 SHA-1)

session.hash_bits_per_character - 定义将二进制散列数据转换为可读格式时每个字符中储存几个位(从 4 到 6)

zend.ze1_compatibility_mode - 启用 Zend Engline 1 代(PHP 4)兼容模式

数据库
关于数据库(MySQL 和 SQLite)在 PHP 5 中有些改变。

PHP 5 中不再绑定 MySQL 客户端连接库,因为授权和一些其它问题。更多信息见 FAQ。

有个新扩展库 MySQLi(改良版 MySQL),设计用来工作于 MySQL 4.1 及更高版本之下。

自 PHP 5 起,SQLite 扩展库内置在 PHP 中。SQLite 是一个可嵌入 SQL 数据库引擎,不是客户端连接库用来连接大型数据库服务器(如 MySQL 或 PostgreSQL)的。SQLite 库直接读写磁盘上的数据库文件。

新对象模型

PHP 5 中有个新对象模型(Object Model)。PHP 处理对象的方式完全重写了,允许更佳性能和更多特性。之前版本的 PHP,对象处理方式和原始类型(例如整型和字符串)相同。此方法的缺点是当变量被赋值或作为参数传递给方法时语义上整个对象都被拷贝。在新方法中,对象通过句柄引用,而不是值(可以将句柄当成是对象的标识符)。

很多 PHP 程序员根本没意识到旧的对象模型的这种拷贝怪癖,因此大多数 PHP 应用程序拿来就能运行,或者只做很小的修改。

新对象模型的文档见语言参考。

与 PHP 4 的兼容性参见 zend.ze1_compatibility_mode。

错误报告

自 PHP 5 起引进了新常量 E_STRICT,其值为 2048。它提供了对用户代码的协同性和向前兼容性的运行时 PHP 建议,有助于使用户保持最新和最好的编程风格。例如在使用已过时的函数时 STRICT 信息会提出警告。

注: E_ALL 不包括 E_STRICT,因此其默认未激活。
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]