面向 PHP 5.3 的 PHP 开发——DEPREC…

PHP 5.3 开始,为了更好的向 PHP 的未来版本(PHP6) 过渡,将未来不再支持的函数标记为 DEPRECATED。在代码中使用这些函数,将毫不留情的在页面中显示警告信息:“使用了过时的函数…”,诸如此类。

那么如何面向未来,让现有的 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+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Fix `ereg is deprecated` errors in PHP 5.3If 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 ereg family of functions, which are gone for good, as they were slower and felt less familiar than the alternative Perl-compatible preg family.To migrate ereg():ereg('\.([^\.]*$)', $this->file_src_name, $extension);


becomes


preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);


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 eregi functions (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 i after 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.


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wxlkeepmoving/archive/2010/10/17/5947101.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值