Home > Article > Backend Development > Summary of new features of PHP5.3, new features of php5.3_PHP tutorial
This article summarizes and analyzes the new features of PHP5.3. Share it with everyone for your reference, the details are as follows:
1. Namespace solves the problem of class, function and constant name conflicts
2. Static binding. When inheriting, the parent class can directly call the subclass to override the method of the parent class
class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // 后期静态绑定从这里开始 } } class B extends A { public static function who() { echo __CLASS__; } } B::test();
3. Anonymous functions, also called closure functions, allow you to temporarily create a function without a specified name. Most commonly used as callback function
//匿名函数做回调函数 uasort($arr ,function($a, $b){ })
Closure functions can also be used as the value of variables
$fn = function ($a) { echo $a; }; $fn(1);
PHP will automatically convert the expression into an object instance of the built-in class Closure
$fn = function ($a) { echo $a; }; ee($fn); /** * Closure Object ( [parameter] => Array ( [$a] => ) ) */
Anonymous functions are currently implemented through the Closure class. It is currently unstable and not suitable for official development
3. ?: operator
$a = 0; $b = 2; ee($a ?: $b); # 2 类似js中的 ||
4. New constant __DIR_
5. The new garbage collection mechanism solves the problem of circular references
gc_enable(); // 激活循环引用收集器,默认开启 var_dump(gc_collect_cycles()); // 强制回收已无效的变量 gc_disable(); // 禁用GC
Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction Tutorial on PHP Basic Syntax", "Summary of PHP Error and Exception Handling Methods" and "Summary of Common PHP Functions and Techniques"
I hope this article will be helpful to everyone in PHP programming.