• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

【转】PHP5.3亲测精髓特性整理

php 搞代码 3年前 (2022-01-24) 14次浏览 已收录 0个评论

【转】PHP5.3亲测精华特性整理
1、引入 __DIR__ magic常量 定位脚本的目录

    echo __DIR__."|".dirname(__FILE__);

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

    echo (1 ?: 0) ."|". ("" ?: 0)."|". var_export(array() ?: array(1), true);

3、类添加__callStatic方法,等价于 __call(),但它是为调用静态方法准备的。下面来看示例:

    class xxx{         function __call($func, $args){             echo "function name:".$func;             echo "</br>";             echo "args:".var_export($args, true);         }         static function __callStatic($fun<em>¥本文来%源[email protected]搞@^&代*@码)网5</em><strong>搞gaodaima代码</strong>c, $args){             echo "static function name:".$func;             echo "</br>";             echo "static args:".var_export($args, true);         }     }     $x = new xxx();     $x->testfunc("oo");     echo "</br>";     xxx::teststatic("xx");

4、动态的调用静态的方法

    class xx{         static function test(){             echo __METHOD__;         }     }     $c = "xx";     $m = "test";     $c::$m();

5、延迟静态绑定(静态处理从编译时延迟到执行时)

class A{     static function whoami(){         echo __CLASS__;     }      static function say1(){         self::whoami();     }      static function say2(){         static::whoami();     } }  class B extends A{     static function whoami(){         echo __CLASS__;     } }  class C extends A{     static function whoami(){         echo __CLASS__;     } }  B::say1(); C::say2();

6、MySQLInd (高速的专门为PHP设计的MySQL调用库接口)
7、INI Magic
8、扩展的 OpenSSL 函数
9、SPL(Standard PHP Library) 优化
10、时间处理进行扩展了和添加

        $date = strtotime("11-05-17 00:00:00");     echo date("Y-m-d", $date)."|";      $date = date_create_from_format("y-m-d", "11-05-17");     echo $date->format("Y-m-d");

11、垃圾回收器(为复杂和长时间运行脚本的执行结束周期释放内存的清理)

        gc_enable(); // 允许垃圾回收     var_dump(gc_enabled()); // true      var_dump(gc_collect_cycles()); // 某个元素的清理      gc_disable(); // 禁止垃圾回收

12、NOWDOC(不对HEREDOC进行转译)

    $fubar = "xxx";      $xbar = <<<ONE         this is HEREDOC $fubar     ONE;      $abar = <<<'TWO'         this is NOWDOC $fubar     TWO;     echo $xbar."|".$abar;

13、匿名函数体验

    $ofun = create_function('$a, $b', 'return $a*$b;'); //不要忘了分号,传统方法     echo $ofun(2, 5);      $nfun = function ($a, $b){         return $a*$b;     };//不要忘了分号,现代方法      echo $nfun(3, 5);

14、闭包体验

    $arr = array(1, 2, 3, 5, 6, 7);     $res = array_filter($arr, function($v) { return $v >4; });     var_dump($res);      function countnum(){         $num = 0;         $testt = function() use(&$num){             ++$num;             echo $num;         };         return $testt;     }      $t = countnum();     $t();     $t();

15、命名空间

    namespace xxx; //第一个namespace需要在所有输出之前     function t(){         return __NAMESPACE__;     }      namespace xxx;      function t(){         return __NAMESPACE__;     }      echo \xx\t()."|";     echo \xxx\t();

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:【转】PHP5.3亲测精髓特性整理

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址