蓝冰大侠 发表于 2011-12-21 08:44

[转]PHP5.3语言特性


                <p>性能提升</p><p>php 5.3的总体性能提升了5 - 15%</p><p class="code">md5()快了10-15% <br>Better stack implementation in the engine <br>Constants移到read-only内存里 <br>exception处理过程改进(简化,opcodes更少) <br>(require/include)_once改进,去掉重复open <br>Smaller binary size &amp; startup size with gcc4 </p><p>新语言特性</p><p>__DIR__</p><p>在5.3以前,为了获得当前脚本的目录,需要一次函数调用</p><p class="code">CODE: <br>echo dirname(__FILE__); // &lt; PHP 5.3 </p><p>在5.3,只需要一个魔术常量__DIR__就解决了。</p><p class="code">CODE: <br>echo __DIR__; // &gt;= PHP 5.3 </p><p>?:操作符</p><p>便捷的?:操作符,可以从两个值/表达式中快速取得非空值。</p><p class="code">CODE: <br>$a = true ?: false; // true<br>$a = false ?: true; // true<br>$a = "" ?: 1; // 1<br>$a = 0 ?: 2; // 2<br>$a = array() ?: array(1); // array(1);<br>$a = strlen("") ?: strlen("a"); // 1 </p><p>__callStatic()</p><p>新增了魔术方法__callStatic,功能和__call类似,但是仅对static方法有效。</p><p class="code">CODE: <br>class helper {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static function __callStatic($name, $args) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $name.'('.implode(',', $args).')';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>}<br>helper::test("foo","bar"); // test(foo,bar) </p><p>动态调用static方法</p><p>动态的调用静态方法?动静结合。</p><p class="code">CODE: <br>class helper {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static function foo() { echo __METHOD__; }<br>}<br>$a = "helper";<br>$b = "foo";<br>$a::$b(); // helper::foo </p><p>Late Static Binding</p><p>不知道怎么译,可能留个原文更容易理解。静态方法的事件处理时机有变化,以前是在编译期处理,现在是执行期间处理。</p><p>在php 5.3之前,下面的代码会输出一个A,但是这不是咱们要的,whoami方法已经在class B中重新定义,它本该输出B才符合咱们想当然的思维。</p><p class="code">CODE: <br>class A {<br>&nbsp;&nbsp; public static function whoami() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo __CLASS__;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; public static function identity() {<br>&nbsp;&nbsp;&nbsp;&nbsp; self::whoami();<br>&nbsp;&nbsp; }<br>}<br>class B extends A {<br>&nbsp;&nbsp; public static function whoami() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo __CLASS__;<br>&nbsp;&nbsp; }<br>}<br>B::identity(); // A &lt;-- PHP &lt;5.3 </p><p>下面代码中使用了static::whoami()来调用静态方法。php 5.3之后,由于__CLASS__是在执行期被处理,那么这个例子中能顺利抓到class B。</p><p class="code">CODE: <br>class A {<br>&nbsp;&nbsp; public static function whoami() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo __CLASS__;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; public static function identity() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static::whoami();<br>&nbsp;&nbsp; }<br>}<br>class B extends A {<br>&nbsp;&nbsp; public static function whoami() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo __CLASS__;<br>&nbsp;&nbsp; }<br>}<br>B::identity(); // B &lt;--&gt;= PHP 5.3 </p><p>mysqlnd</p><p>mysqlnd成为php 5.3中的默认mysql驱动,它有如下优点:</p><p>mysqlnd更容易编译: 因为它是php源码树的一个组成部分 <br>mysqlnd和php内部机制结合更紧密,是优化过的mysql驱动 <br>mysqlnd更节省内存,从测试结果来看,比传统的mysql扩展节省40%的内存 <br>mysqlnd更快 <br>mysqlnd提供了丰富的性能统计功能 <br>mysqlnd使用了PHP license以避免不必要的版权纠纷 </p><p>这个改动应同时对mysql和pdo_mysql扩展生效。</p><p>mysqlnd是什么</p><p>mysqlnd是mysql原装的php驱动</p><p>但是PDO_MySQL暂时还不支持mysqlnd,目前只有mysql(i)扩展可以用到</p>
               
               
               
               
               
               
               
页: [1]
查看完整版本: [转]PHP5.3语言特性