PHP5.3.x的新功能

1.支持命名空间

 

01 <?
02 namespace namespaceDemo;
03 class demoClass
04 {
05 function echoValue()
06 {
07 echo 'i am "\namespaceDemo\demoClass->echoValue"';
08 }}$test new \namespaceDemo\demoClass();
09 $test->echoValue();
10 /**
11 * output:
12 * i am "\namespaceDemo\demoClass->echoValue"
13 */
14 ?>

2.支持GOTO语句

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

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

 

01 <?php
02 for$i = 1; $i <= 100; $i++ )
03 {
04 if$i % 20 == 0 )
05 goto end;
06 }end:
07 echo "我是{$i}我是第一个被20整除的数!";
08 /**
09 * output:
10 * 我是20我是第一个被20整除的数!
11 */
12 ?>

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

 

01 <?php
02 /**
03 * __callStatic魔术方法使用方式
04 */
05 class callStatic
06 {
07 public static function  __callStatic( $name$arguments )
08 {
09 echo "<b>function name:</b>";
10 echo $name '<br>';
11 echo "<b>function arguments:</b>";
12 print_r( $arguments );
13 }}$callStatic new callStatic();
14 echo $callStatic::notExistsFun( array'param1' 'param2' ) );
15 /**
16 * output:
17 * function name:notExistsFun
18 * function arguments:Array ( [0] => Array ( [0] => param1 [1] => param2 ) )
19 */
20 ?>

4.新的匿名魔术方法 __invoke.

如果对象实现了__invoke魔术方法就可将其作为函数直接调用,实例化对象之后可用匿名函数的形式直接调用。

 

01 <?php
02 class invokeDemo
03 {
04 public function __invoke( $param1 $param2 )
05 {
06 print_r( array$param1 $param2 ) );
07 echo '<br>  this is invoke magic function';
08 }}$invokeDemo new invokeDemo();
09 $invokeDemo'param1String' array( 1 , 2 , 3) );
10 /**
11 * output:
12 * Array ( [0] => param1String [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
13 * this is invoke magic function
14 */
15 ?>

5.动态调用静态属性。

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

 

01 <?php
02 /*
03 * To change this template, choose Tools | Templates
04 * and open the template in the editor.
05 */
06 class dynamicCallStatic
07 {
08 public static function callStaticFunc()
09 {
10 echo 'i am callStaticFunc function';
11 }}$funcName 'callStaticFunc';
12 dynamicCallStatic::$funcName();
13 /**
14 * output:
15 * i am callStaticFunc function
16 */
17 ?>

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

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

 

01 <?php
02 $city  'dalian';
03 $closure function$name use$city ){ echo $name'-' $city; };
04 $closure'jucky' );
05 /**
06 * output:
07 * jucky-dalian
08 */
09 ?>
10 <?php
11 $arr array( 3, 2, 5, 6, 1 );
12 usort(
13 $arr,
14 function$a$b )
15 {
16 if$a == $b )
17 return 0;
18 return ($a $b) ? -1 : 1;
19 });
20 print_r( $arr );
21 /**
22 * output:
23 * Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 )
24 */

7.新的三元操作符

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

 

01 <?php
02 $result = true;
03 echo $result == true ?: 'error code:20';
04 /**
05 * output:
06 * 1
07 */
08 $result = false;
09 echo $result == true ?: 'error code:20';
10 /**
11 * output:
12 * error code:20
13 */
14 ?>

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

 

1 <?php
2 const PLATFORM_NAME = 'Linux';
3 echo PLATFORM_NAME;
4 /**
5 * output:
6 * Linux
7 */
8 ?>

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

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

 

1 <?php
2 $arr array( 1 , 2 , 3 );
3 echo json_encode( $arr ) . '<br>';
4 echo json_encode( $arr , JSON_FORCE_OBJECT );
5 /**
6 * output:
7 * [1,2,3]
8 * {"0":1,"1":2,"2":3}*/
9 ?>

10.默认启用SPL支持。

 
SPL提供了很多激动人心的特性,具体包括数据结构类、迭代类、接口、异常、文件等通用功能类。
 
数据结构提供了双向链表、栈、队列、堆、优先队列、固定大小数组、对象存储器, 但效率是否高于数组实现需要在应用内具体测试,方才测试队列类的效能发现低于原生实现, 不过固定数组的效率还是很可观的,下面代码是对固定大小数组的一个测试, 速度快了近1倍,详细代码如下:
 
1 $i=1000000;
2 $spl new SplFixedArray( 1000000 );
3 while ($i--) {
4 $spl[$i] = 'SM';
5 }
 
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:
 
array_replace() - Replaces elements from passed arrays into one array.
array_replace_recursive() - Recursively replaces elements from passed arrays into one array.
class_alias() - Creates an alias for a user defined class.
forward_static_call() - Call a user function from a method context.
forward_static_call_array() - Call a user function from a method context, with the arguments contained in an array.
gc_collect_cycles() - Forces collection of any existing garbage cycles.
gc_disable() - Deactivates the circular reference collector.
gc_enable() - Activates the circular reference collector.
gc_enabled() - Returns the status of the circular reference collector.
get_called_class() - Return the name of the class a static method is called in.
gethostname() - Return the current host name for the local machine.
header_remove() - Removes an HTTP header previously set using the header() function.
lcfirst() - Make a string's first character lowercase.
parse_ini_string() - Parse a configuration string.
quoted_printable_encode() - Convert an 8 bit string to a quoted-printable string.
str_getcsv() - Parse a CSV string into an array.
stream_context_set_default() - Set the default stream context.
stream_supports_lock() - Return TRUE if the stream supports locking.
stream_context_get_params() - Retrieve parameters from a stream context.
streamWrapper::stream_cast() - Retrieve the underlying stream resource.
streamWrapper::stream_set_option() - Change stream options
Date/Time:
 
date_add() - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object.
date_create_from_format() - Returns a new DateTime object formatted according to the given format.
date_diff() - Returns the difference between two DateTime objects.
date_get_last_errors() - Returns the warnings and errors from the last date/time operation.
date_parse_from_format() - Get infoformation about a given date.
date_sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object.
timezone_version_get() - Returns the version of the timezonedb.
GMP:
 
gmp_testbit() - Tests whether a bit is set.
Hash:
 
hash_copy() - Copy hashing context.
IMAP:
 
imap_gc() - 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.
JSON:
 
json_last_error() - Returns the last JSON error that occurred.
MySQL Improved:
 
mysqli_fetch_all() - Fetches all result rows as an associative array, a numeric array, or both.
mysqli_get_connection_stats() - Returns statistics about the client connection.
mysqli_poll() - Poll connections.
mysqli_reap_async_query() - Get result from async query.
OpenSSL:
 
openssl_random_pseudo_bytes() - Returns a string of the given length specified, filled with pseudo-random bytes.
PCNTL:
 
pcntl_signal_dispatch() - Calls signal handlers for pending signals.
pcntl_sigprocmask() - Sets and retrieves blocked signals.
pcntl_sigtimedwait() - Wait for signals with a timeout.
pcntl_sigwaitinfo() - Wait for signals.
PCRE:
 
preg_filter() - Perform a regular expression search and replace, reutrning only results which matched the pattern.
Semaphore:
 
msg_queue_exists() - Check whether a message queue exists.
shm_has_var() - 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:
 
acosh()
asinh()
atanh()
expm1()
log1p()
原有函数的参数变更:
 
PHP Core:
 
clearstatcache() - Added clear_realpath_cache and filename .
copy() - Added a stream context parameter, context .
fgetcsv() - Added escape .
ini_get_all() - Added details .
The mail() function now supports logging of sent email. (Note: This only applies to email sent through this function.)
nl2br() - Added is_xhtml .
parse_ini_file() - Added scanner_mode .
round() - Added mode .
stream_context_create() - Added params .
strstr() and stristr() - Added before_needle .
json:
 
json_encode() - Added options .
json_decode() - Added depth .
Streams:
 
stream_select(), stream_set_blocking(), stream_set_timeout(), and stream_set_write_buffer() now work with user-space stream wrappers.
sybase_ct:
 
sybase_connect() - Added new .
New method parameters in PHP 5.3.0:
 
PHP Core:
 
Exception::__construct - Added previous .
参考文档:
PHP V5.3 中的新特性 John Mertic  http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new1/index.html
Manual - Backward Incompatible Changes PHP  http://cn.php.net/manual/zh/migration53.incompatible.php
Manual - New features http://cn.php.net/manual/en/migration53.new-features.php
drupal - PHP 5.3 Compatibility  http://drupal.org/node/360605
本文出处:http://www.cnblogs.com/silvester/archive/2011/04/02/1998745.html

出处: http://blog.duteba.com/technology/article/34.htm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值