面向 PHP 5.3 的 PHP 开发——DEPRECATED
2010-10-17 18:44
204 查看
PHP 5.3 开始,为了更好的向 PHP 的未来版本(PHP6) 过渡,将未来不再支持的函数标记为 DEPRECATED。在代码中使用这些函数,将毫不留情的在页面中显示警告信息:“使用了过时的函数…”,诸如此类。
那么如何面向未来,让现有的 PHP 程序平滑的向下一代 PHP 引擎过渡呢?
define_syslog_variables
register_globals
register_long_arrays
safe_mode
magic_quotes_gpc
magic_quotes_runtime
magic_quotes_sybase
将变量 $LOG_ERR, $LOG_USER 等用常量 LOG_USER, LOG_USER, … 替代
虽然三者的第一个参数都是字符串,表示一个正则表达式。但是 preg_match 用的是 PCRE(Perl 兼容的正则表达式语法):正则表达式的两端用一个符号做边界,如 “/pattern/” 或者 “#pattern#”
eregi 是乎略大小写的匹配,转换为 preg_match,第一个参数,用PCRE的参数来乎略大小写,如:”/pattern/i” 或者 “#pattern#i”
两者的第三个参数返回的匹配的数据结构不同。ereg 的第三个参数在调用结束后,返回的是一个字符串数组,分别为完整匹配字串和各个子匹配字串。preg_match 返回的是二维数组,相当于 ereg 的字串数组中的字串在 preg_match 是一个数组,分别保存匹配值以及匹配位置。
如果要进行多次匹配,PHP 提供了 preg_match_all 函数,其第三个参数的返回值则是一个三维数组;
eregi_replace 到 preg_replace 的替换,在第一个参数的后面增加正则表达式参数。如:”/pattern/i” 或者 “#pattern#i”, …
如果 ereg_replace 的第一个参数不是正则表达式,可以用 str_replace 直接来替换。
对于使用正则表达式切分字串,则使用 preg_split 函数替代。替代过程和 ereg/ereg_replace 类似,只是在第一个正则表达式参数中做文章,将 split 的正则表达式前后加上一个 PCRE 的分隔符号。
将其转换为两次调用,分别是用 mysql_select_db 选择数据库,用 mysql_query 来执行 SQL 查询
使用 mysql_real_escape_string 替代
其功能相当于直接操作全局数组 $_SESSION。可以直接向数组中赋值或者执行相应的 unset 即可实现相关功能
To migrate
becomes
preg_match('//.([^/.]*$)/', $this->file_src_name, $extension);[/code]
Notice that I wrapped the pattern (
To migrate
becomes
Again, I just added delimiters to the pattern.
If you are using
Basically, to make the pattern match characters in a case-insensitive way, append
becomes
Related posts:
Append your blog description to the window title in Wordpress I wanted my homepage title to reveal my writing goals...
Related posts brought to you by Yet Another Related Posts Plugin.
那么如何面向未来,让现有的 PHP 程序平滑的向下一代 PHP 引擎过渡呢?
配置文件迁移
PHP 5.3 开始,配置文件 php.ini 中的一些配置将会在 PHP 执行时显示过时警告,这些配置将在 PHP6中不再存在,相关功能也将关闭。define_syslog_variables
register_globals
register_long_arrays
safe_mode
magic_quotes_gpc
magic_quotes_runtime
magic_quotes_sybase
函数迁移
涉及到的主要的函数迁移如下:删除函数 define_syslog_variables 引用
删除对函数 define_syslog_variables 的引用将变量 $LOG_ERR, $LOG_USER 等用常量 LOG_USER, LOG_USER, … 替代
ereg, eregi 函数用 preg_match 函数替代
这几个函数的函数声明int ereg ( string $pattern , string $string [, array &$regs ] ) int eregi ( string $pattern , string $string [, array &$regs ] ) int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
虽然三者的第一个参数都是字符串,表示一个正则表达式。但是 preg_match 用的是 PCRE(Perl 兼容的正则表达式语法):正则表达式的两端用一个符号做边界,如 “/pattern/” 或者 “#pattern#”
eregi 是乎略大小写的匹配,转换为 preg_match,第一个参数,用PCRE的参数来乎略大小写,如:”/pattern/i” 或者 “#pattern#i”
两者的第三个参数返回的匹配的数据结构不同。ereg 的第三个参数在调用结束后,返回的是一个字符串数组,分别为完整匹配字串和各个子匹配字串。preg_match 返回的是二维数组,相当于 ereg 的字串数组中的字串在 preg_match 是一个数组,分别保存匹配值以及匹配位置。
如果要进行多次匹配,PHP 提供了 preg_match_all 函数,其第三个参数的返回值则是一个三维数组;
ereg_replace, eregi_replace 函数用 preg_replace 函数或者 str_replace 函数来替代
和前面的 ereg 替换为 preg_match 类似,第一个参数要进行转换,头尾增加一个符号,如:”/pattern/” 或者 “#pattern#”, …eregi_replace 到 preg_replace 的替换,在第一个参数的后面增加正则表达式参数。如:”/pattern/i” 或者 “#pattern#i”, …
如果 ereg_replace 的第一个参数不是正则表达式,可以用 str_replace 直接来替换。
split, spliti 函数用 explode 或则 preg_split 函数替代
split 切分字符串,如果无须用到正则表达式,使用 explode 替换是最好不过,速度最快对于使用正则表达式切分字串,则使用 preg_split 函数替代。替代过程和 ereg/ereg_replace 类似,只是在第一个正则表达式参数中做文章,将 split 的正则表达式前后加上一个 PCRE 的分隔符号。
mysql_db_query 函数用 mysql_select_db 和 mysql_query 函数替代
mysql_db_query 在未来版本不再支持将其转换为两次调用,分别是用 mysql_select_db 选择数据库,用 mysql_query 来执行 SQL 查询
mysql_escape_string 函数用 mysql_real_escape_string 函数替代
mysql_escape_string 未来版本不再支持使用 mysql_real_escape_string 替代
session_register 函数,session_unregister,session_is_registered 函数用 $_SESSION 全局变量替代
这三个 session 相关函数未来不再支持其功能相当于直接操作全局数组 $_SESSION。可以直接向数组中赋值或者执行相应的 unset 即可实现相关功能
在代码中查抄过时函数
下面这个脚本可以用于在代码树中查找过时的 PHP 函数#!/bin/sh OPTS="-rHw"; verbose=0 while [ $# -gt 0 ]; do case $1 in -v) verbose=1; shift ;; -q) verbose=0; shift ;; -*) OPTS="$OPTS $1"; shift ;; *) break; ;; esac done if [ $# -eq 0 ]; then echo "Usage $0 [-v] " exit 1 fi [ $verbose -eq 0 ] && OPTS="$OPTS -l" DEPRECATED="call_user_method call_user_method_array define_syslog_variables dl set_magic_quotes_runtime magic_quotes_runtime set_socket_blocking sql_regcase mysql_db_query mysql_escape_string session_register session_unregister session_is_registered eregi? eregi?_replace spliti?" OPTS="$OPTS --include=*.inc --include=*.php --include=*.php5" for item in $DEPRECATED; do echo "##### find deprecated item: $item in $1: #####" grep $OPTS -E "$item/s*$" $* grep $OPTS -E "$item/s*/(" $* echo "" done
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
family of functions, which are gone for good, as they were slower and felt less familiar than the alternative Perl-compatibleFix `ereg is deprecated` errors in PHP 5.3
If you upgraded to PHP 5.3, chances are high you’re going to run into a few warnings or deprecated function messages. An example is the [code]ereg
pregfamily.
To migrate
ereg():
ereg('/.([^/.]*$)', $this->file_src_name, $extension);
becomes
preg_match('//.([^/.]*$)/', $this->file_src_name, $extension);[/code]
Notice that I wrapped the pattern (
/.([^/.]*$)) around
/ /, which are RegExp delimiters. If you find yourself escaping
/too much (for an URL for example), you might want to use the
#delimiter instead.
To migrate
ereg_replace():
$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);
becomes
$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);
Again, I just added delimiters to the pattern.
If you are using
eregifunctions (which are the case-insensitive version of ereg), you’ll notice there’re no equivalent pregi functions. This is because this functionality is handled by RegExp modifiers.
Basically, to make the pattern match characters in a case-insensitive way, append
iafter the delimiter:
eregi('/.([^/.]*$)', $this->file_src_name, $extension);
becomes
preg_match('//.([^/.]*$)/i', $this->file_src_name, $extension);
Related posts:
Append your blog description to the window title in Wordpress I wanted my homepage title to reveal my writing goals...
Related posts brought to you by Yet Another Related Posts Plugin.
相关文章推荐
- PHP5.3开发-解决DEPRECATED错误问题
- PHP5.3开发-解决DEPRECATED错误问题
- PHP面向对象开发基础与实例
- 我的php学习笔记(十四)php的面向对象开发(一)
- 解决php5.3中Deprecated: Assigning the return value of new by reference is deprecated in报错
- PHP面向对象开发的学习(一)
- 面向 PHP 开发人员的 CouchDB 基础知识
- php5.3 中显示Deprecated: Assigning the return value of new by reference is deprecated in 的解决方法
- 我的php学习笔记(十八)php的面向对象开发(五)
- PHP面向对象开发(一)
- windows7下手工搭建Apache2.2 php5.3 Mysql5.5开发环境
- 学习PHP C扩展之面向对象开发方式 (转)
- php5.3 中显示Deprecated: Assigning the return value of new by reference is deprecated in 的解决方法
- 我的php学习笔记(十七)php的面向对象开发(四)
- PHP面向对象开发,关于调用父类构造器的问题
- PHP面向对象开发的学习(一)
- PHP面向对象开发的学习(三)(PHP100第二十讲)
- 面向网络的PHP开发第26章
- 学习PHP C扩展之面向对象开发方式