Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4176086
  • 博文数量: 601
  • 博客积分: 15410
  • 博客等级: 上将
  • 技术积分: 6884
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-16 08:11
个人简介

独学而无友,则孤陋而寡闻!

文章分类

全部博文(601)

文章存档

2020年(1)

2018年(4)

2017年(7)

2016年(42)

2015年(25)

2014年(15)

2013年(36)

2012年(46)

2011年(117)

2010年(148)

2009年(82)

2008年(37)

2007年(41)

分类: Python/Ruby

2011-05-07 22:51:03

PHP5.3新功能与不兼容特性整理

1.支持命名空间

      PHP5.3之后的版本引入了名称空间的支持,此举的好处是不同模块之间分隔方式变得天然纯正,以往PHP要实现不同模块之间的划分通常会各为其政,有 类PEAR或ZendFramework的, 有像Drupal以模块区分等等,在已有项目内引入了第三方库经常会担心是否有全局名称的冲突,PHP5.3后这种担心可大大减低, 其为我们提供了一种主流解决方案。

      关于此特性也颇具争议有人认为PHP的特点就是简单,在PHP5之后越加复杂违背了最初的理念,不过鄙人拙见认为语言就是应对解决问题而生有更好的解决方式就应该提倡。

echoValue"'; } } $test = new \namespaceDemo\demoClass(); $test->echoValue(); /** * output: * i am "\namespaceDemo\demoClass->echoValue" */ ?>

2.支持GOTO语句

      GOTO语句曾经颇受争议, 后来经过大师们很多次较量后才最终确认合理使用有益无害,PHP5.3之后也引入了此语句。

3.新的静态魔术方法__callStatic.

      5.2支持通过魔术方法处理不存在方法的调用, 但其只支持非静态方法, 5.3版本引入了静态魔术方法。

function name:"; echo $name . '
'; echo "function arguments:"; print_r( $arguments ); } } $callStatic = new callStatic(); echo $callStatic::notExistsFun( array( 'param1' , 'param2' ) ); /** * output: * function name:notExistsFun * function arguments:Array ( [0] => Array ( [0] => param1 [1] => param2 ) ) */ ?>
4.新的匿名魔术方法 __invoke.
      如果对象实现了__invoke魔术方法就可将其作为函数直接调用,实例化对象之后可用匿名函数的形式直接调用。
this is invoke magic function'; } } $invokeDemo = new invokeDemo(); $invokeDemo( 'param1String' , array( 1 , 2 , 3) ); /** * output: * Array ( [0] => param1String [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) * this is invoke magic function */ ?>

 

5.动态调用静态属性。

  PHP5.2之前静态方法是不支持用变量指定方法名称并动态调用的, 5.3之后引入了此功能。

6.支持匿名函数(lumbda).

      5.3之后引入了匿名函数,对Javascript了解的人对其并不陌生它占了JS的重头戏,灵活运用匿名函数会带来很多便利,PHP的匿名函数作用域 和函数的作用域相同,不过可以通过内置语法USE传入全局变量,当然也可以在函数内部使用global或$GLOBALS调用全局变量。

1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 ) */

 

7.新的三元操作符

      新三元操作符?:在判断表达式结果为TRUE时会返回此判断表达式的结果即?:之前的值, False时返回?:之后的值。

 

8.全局空间内const代替define()函数可直接使用

 

9.json_encode支持强制转换对象JSON

      5.3后的json_enocde可通过参数强制转换数组为对象形式JSON。

'; echo json_encode( $arr , JSON_FORCE_OBJECT ); /** * output: * [1,2,3] * {"0":1,"1":2,"2":3} */ ?>

 

10.默认启用SPL支持。

     SPL提供了很多激动人心的特性,具体包括数据结构类、迭代类、接口、异常、文件等通用功能类。

     数据结构提供了双向链表、栈、队列、堆、优先队列、固定大小数组、对象存储器, 但效率是否高于数组实现需要在应用内具体测试,方才测试队列类的效能发现低于原生实现, 不过固定数组的效率还是很可观的,下面代码是对固定大小数组的一个测试, 速度快了近1倍,详细代码如下:

$i=1000000; $spl = new SplFixedArray( 1000000 ); while ($i--) { $spl[$i] = 'SM'; }

SplFixedArray数组运行时间与内存使用大小

Running time:0.2420928478241
Memory usage:52324152

Array数组运行时间与内存使用大小:

Running time:0.42152786254883
Memory usage:100517992

11.延迟静态绑定。

12.循环垃圾收集, 能够发现对象的循环引用并自动回收内存

13.支持phar归档

不兼容的特性:

1.atsort,natcasesort,usort,uasort,array_flip等数组函数不支持传入对象。

2.魔术方法必须声明为公共属性

3.从PECL移除的库,ncurses, fpdf, dbase, fbsql, ming.

4.废弃的tick, ereg正则。

5.namespace,Closure变成了保留值

6.http Stream流支持200-399全部状态

7.去除了magic quotes 和 register globals特性

8.出错提示信息与5.2版本相比有变化

9.以下配置项在PHP5.3将产生警告

define_syslog_variables
register_globals
register_long_arrays
safe_mode
magic_quotes_gpc
magic_quotes_runtime

        magic_quotes_sybase 

 
新的函数:

PHP Core:

  • – Replaces elements from passed arrays into one array.
  • array_replace_recursive() – Recursively replaces elements from passed arrays into one array.
  • – Creates an alias for a user defined class.
  • – Call a user function from a method context.
  • – Call a user function from a method context, with the arguments contained in an array.
  • – Forces collection of any existing garbage cycles.
  • – Deactivates the circular reference collector.
  • – Activates the circular reference collector.
  • – Returns the status of the circular reference collector.
  • – Return the name of the class a static method is called in.
  • – Return the current host name for the local machine.
  • – Removes an HTTP header previously set using the function.
  • – Make a string’s first character lowercase.
  • – Parse a configuration string.
  • – Convert an 8 bit string to a quoted-printable string.
  • – Parse a CSV string into an array.
  • – Set the default stream context.
  • – Return TRUE if the stream supports locking.
  • – Retrieve parameters from a stream context.
  • – Retrieve the underlying stream resource.
  • – Change stream options

:

  • – Adds an amount of days, months, years, hours, minutes and seconds to a object.
  • – Returns a new object formatted according to the given format.
  • – Returns the difference between two objects.
  • – Returns the warnings and errors from the last date/time operation.
  • – Get infoformation about a given date.
  • – Subtracts an amount of days, months, years, hours, minutes and seconds from a object.
  • – Returns the version of the timezonedb.

:

  • – Tests whether a bit is set.

:

  • – Copy hashing context.

:

  • – Clears IMAP cache.
  • imap_utf8_to_mutf7() – Encode a UTF-8 string to modified UTF-7.
  • imap_mutf7_to_utf8() – Decode a modified UTF-7 string to UTF-8.

:

  • – Returns the last JSON error that occurred.

:

  • – Fetches all result rows as an associative array, a numeric array, or both.
  • – Returns statistics about the client connection.
  • – Poll connections.
  • – Get result from async query.

:

  • – Returns a string of the given length specified, filled with pseudo-random bytes.

:

  • – Calls signal handlers for pending signals.
  • – Sets and retrieves blocked signals.
  • – Wait for signals with a timeout.
  • – Wait for signals.

:

  • – Perform a regular expression search and replace, reutrning only results which matched the pattern.

:

  • – Check whether a message queue exists.
  • – Checks whether a specific key exists inside a shared memory segment.

The following functions are now natively implemented, making them available on all operating systems which can run PHP:

原有函数的参数变更:

PHP Core:

  • – Added clear_realpath_cache and filename .
  • – Added a stream context parameter, context .
  • – Added escape .
  • – Added details .
  • The function now supports logging of sent email. (Note: This only applies to email sent through this function.)
  • – Added is_xhtml .
  • – Added scanner_mode .
  • – Added mode .
  • – Added params .
  • and – Added before_needle .

:

  • – Added options .
  • – Added depth .

:

  • , , , and now work with user-space stream wrappers.

:

  • – Added new .

New method parameters in PHP 5.3.0:

PHP Core:

  • – Added previous

 

已使用5.3的应用:
drupal7.0
zend Home
 
参考文档:
PHP V5.3 中的新特性 John Mertic  http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new1/index.html
Manual – Backward Incompatible Changes PHP  
Manual – New features
drupal – PHP 5.3 Compatibility  


原文链接:
阅读(2301) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~