您的位置:首页 > 编程语言 > PHP开发

PHP5.3新特性之语言特性

2009-07-16 16:05 302 查看
新的语言特性

1) __DIR__

* 引入 __DIR__ magic常量 定位脚本的目录

echo dirname(__FILE__); // < PHP 5.3

   

/* vs */

   

echo __DIR__; // >= 5.3

2) ?:  操作符

* 允许从2个值的or/and表达式快速的获取一个非空的值

   

$a = true ?: false; // true;

$a = false ?: true; // true;

$a = "" ?: 1; // 1

$a = 0 ?: 2; // 2

$a = array() ?: array(1); // array(1);

$a = strlen("") ?: strlen("a"); // 1

  

3) __callStatic()

   

* 等价于 __call() , 但它是为调用静态方法准备的

   

class helper

{

static function __callStatic($name, $args){

echo $name.'('.implode(',' $args).')';

}   

}

   

helper::test("foo", "bar"); // test(foo,bar);

// 动态的函数/方法调用有点慢...

4) 动态的调用静态方法

* php 现在允许 动态的调用静态的方法

   

class helper

{

static function foo(){

echo __METHOD__;`

}   

}

   

$a = "helper";

$b = "foo";

   

$a::$b(); // helper::foo

// 动态的函数/方法调用有点慢...

5) 延迟静态绑定

* 静态处理从编译时延迟到执行时

   

class A

{

public static function whoami(){

echo __CLASS__;   

}

   

public static function identity(){

self::whoami();   

}

}

   

class B extends A

{

public static function whoami(){

echo __CLASS__;

}

}

   

B::identity(); // A <-- php < 5.3

   

   

class A

{

public static function whoami(){

echo __CLASS__;   

}

   

public static function identity(){

static::whoami();   

}

}

   

class B extends A

{

public static function whoami(){

echo __CLASS__;

}

}

   

B::identity(); // B <-- php >= 5.3

* 小心使用操作码缓存,没有向后兼容

6) MySQLInd

* 特殊的,高速的专门为PHP设计的MySQL调用库接口

* 更好的性能

* 内存的使用优化

* 内置的驱动(不是适应性的再次扩展)

* Many future options due to tight integration with PHP

* 目前还没有PDO_MySQL 支持 mysql(i) only for now

7) INI Magic

* CGI/FastCGI 支持".htaccess" 形式的INI控制

* 用户可以自己设定每个目录的INI在php.ini中通过[PATH=/var/www/domain.com]设定

* 优化错误处理

* 允许用户使用INI变量和常量任何定义的INI文件中

* 其他几个小的优化

用户自定义的php.ini(.htaccess) 文件名. 默认为".user.ini"

user_ini.filename = ".user.ini"

禁止这个特性 设置这个选项为空值

用户自定义php.ini 的缓存失效期(time-to-live) 秒数. 默认is 300s (5分钟)

user_ini.cache_ttl = 300s

   

[PATH=/var/www/domain.com]

variables_order = GPC

safe_mode = 1

[my varibles]

somevar = "1234"

anothervar = ${somevar}; anothervar == somevar

[ini arrays]

foo[bar] = 1

foo[123] = 2

foo[] = 3

8) 扩展的 OpenSSL 函数

* 使用 OpenSSL Digest 函数

foreach (openssl_get_md_methods() as $d) {// MD4, MD5, SHA512... (12 all in all)

echo $d. " - ". openssl_digest("foo", "md5"); // acbd18db4cc2f85cedef654fccc4a4d8

}

* 使用 OpenSSL 加密函数

// BF-CBC, AES-256 CFB1... (54 all in all)

foreach(openssl_get_cipher_methods() as $v) {

$val = openssl_encrypt("value", $v, "secret");

openssl_decrypt($val, $v, "secret"); // value

}

* 扩展的 openssl_pkey_new() 和 openssl_pkey_get_details()

函数 允许访问 内部的 DSA, RSA 和 DH 密匙.

其目标在PHP中实现一个简单的OpenId

9) SPL(Standard PHP Library) 优化

* 优化嵌套的目录迭代次数由文件系统迭代

* 引入 GlobIterator

* 各种各样的数据结构类: 双链表, 堆栈, 队列, 堆, 小型堆, 大型堆, 优先级队列

* 其他的很绕口的一些特征

10) 时间处理进行扩展了和添加

* 可控制的 strtotime() 由 date_create_from_format()实现

   

$date = strtotime("08-01-07 00:00:00");

var_dump(date("Y-m-d", $date)); // string(10) "2008-01-07"

$date = date_create_from_format("m-d-y", "08-01-07");

var_dump($date->format('Y-m-d')); // string(10) "2007-08-01"

* 添加了 date_get_last_errors(),并且返回时间语法分析的错误和警告

array(4) {

["warning_count"] => int(0)

["warnings"] => array(0) { }

["error_count"] => int(2)

["errors"]=>

array(2) {

[2]=> string(40) "The separation symbol could not be found"

[6]=> string(13) "Trailing data"

}

}

11) getopt() 优化

* 影响 Windows 平台

* 本地的执行不依赖于本地getopt()实现.

* 跨平台支持长选项 (--option)

// input: --a=foo --b --c

var_dump(getopt("", array("a:","b::","c")));

/* output: array(3) {

["a"]=>

string(3) "foo"

["b"]=>

bool(false)

["c"]=>

bool(false)

} */

12) XSLT Profiling

* 引入 Xslt Profiling 通过 setProfiling()实现

$xslt = new xsltprocessor();

$xslt->importStylesheet($xml);

$xslt->setProfiling("/tmp/profile.txt");

$xslt->transformToXml($dom);

   

Resulting In:

number match name mode Calls Tot 100us Avg

0 date 5 58   11

Total 5 58

13) E_DEPRECATED 标记

* 怎么样将一个php发行为一个没有错误的模式? 废弃

* E_DEPRECATED用来指定废弃的功能,或许未来的版本中会消除。

14) 垃圾回收器

* 为复杂和长时间运行脚本的执行结束周期释放内存的清理

gc_enable(); // 允许垃圾回收

var_dump(gc_enabled()); // true

var_dump(gc_collect_cycles()); // 某个元素的清理

gc_disable(); // 禁止垃圾回收

   

15) NOWDOC

* 一个 HEREDOC 不再进行转译

HEREDOC

$foo = <<<ONE

this is $fubar

ONE;

/* string(10) "this is" */

   

NOWDOC

$bar = <<<‘TWO’

this is $fubar

TWO;

/* string(16) "this is $fubar" */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息