1.命名空间
<?php
namespace Test;
class Demo {
public static function test() {
echo __CLASS__;
}
}
Demo::test();
//输出Test\Demo
?>
2.延迟静态绑定
<?php
//在PHP5中,我们可以在类中通过self关键字或者__CLASS__来判断或调用当前类。
//但有一个问题,如果我们是在子类中调用,得到的结果将是父类。因为在继承父类的时候,静态成员就已经被绑定了。
namespace Test1;
class Super {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class Demo extends Super {
public static function who() {
echo __CLASS__;
}
}
Demo::test();
//输出 Test1\Super
namespace Test2;
//PHP 5.3中增加了一个static关键字来引用当前类,即实现了延迟静态绑定:
class Super {
public static function who() {
echo __CLASS__;
}
public static function test() {
//static关键字实现了延迟静态绑定
static::who();
}
}
class Demo extends Super {
public static function who() {
echo __CLASS__;
}
}
Demo::test();
//输出 Test2\Demo
?>
3.新增两个魔术方法
<?php
//PHP中原本有一个魔术方法__call(),当代码调用对象的某个不存在的方法时该魔术方法会被自动调用。
//新增的__callStatic()方法则只用于静态类方法。
//当尝试调用类中不存在的静态方法时,__callStatic()魔术方法将被自动调用。
class Test {
public function __call($method, $params) {
echo '调用对象方法:【'.$method.'】'.'参数为:'.implode(',', $params);
}
public static function __callStatic($method, $params) {
echo '调用静态方法:【'.$method.'】'.'参数为:'.implode(',', $params);
}
//以函数形式来调用对象时,__invoke()方法将被自动调用。
public function __invoke($obj) {
var_dump($obj, is_callable($obj));
}
}
$obj = new Test();
$obj->run('call method');
Test::run('call method');
$obj($obj);
?>
4.在类外也可使用const来定义常量
<?php
//PHP中定义常量通常是用这种方式
define("CONSTANT", "Hello World");
//PHP5.3新增了一种常量定义方式
const CONSTANT = 'Hello World';
?>
5.闭包、Lambda函数
<?php
//在PHP中,我们也可以通过create_function()在代码运行时创建函数。
//lambda函数
$test = function($name) {
printf("Hello %s!\r\n", $name);
};
$test('PHP');
class Test {
function __call($method, $params) {
if ($method == __CLASS__) return;
if ($this->$method instanceof Closure) {
$func = $this->$method;
return call_user_func_array($func,$params);
}
}
function run() {
$this->handler();
}
}
$test = new Test();
$test->handler = function() {
printf("Hello World!\r\n");
};
$test->run();
?>
<?php
namespace Test;
class Demo {
public static function test() {
echo __CLASS__;
}
}
Demo::test();
//输出Test\Demo
?>
2.延迟静态绑定
<?php
//在PHP5中,我们可以在类中通过self关键字或者__CLASS__来判断或调用当前类。
//但有一个问题,如果我们是在子类中调用,得到的结果将是父类。因为在继承父类的时候,静态成员就已经被绑定了。
namespace Test1;
class Super {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class Demo extends Super {
public static function who() {
echo __CLASS__;
}
}
Demo::test();
//输出 Test1\Super
namespace Test2;
//PHP 5.3中增加了一个static关键字来引用当前类,即实现了延迟静态绑定:
class Super {
public static function who() {
echo __CLASS__;
}
public static function test() {
//static关键字实现了延迟静态绑定
static::who();
}
}
class Demo extends Super {
public static function who() {
echo __CLASS__;
}
}
Demo::test();
//输出 Test2\Demo
?>
3.新增两个魔术方法
<?php
//PHP中原本有一个魔术方法__call(),当代码调用对象的某个不存在的方法时该魔术方法会被自动调用。
//新增的__callStatic()方法则只用于静态类方法。
//当尝试调用类中不存在的静态方法时,__callStatic()魔术方法将被自动调用。
class Test {
public function __call($method, $params) {
echo '调用对象方法:【'.$method.'】'.'参数为:'.implode(',', $params);
}
public static function __callStatic($method, $params) {
echo '调用静态方法:【'.$method.'】'.'参数为:'.implode(',', $params);
}
//以函数形式来调用对象时,__invoke()方法将被自动调用。
public function __invoke($obj) {
var_dump($obj, is_callable($obj));
}
}
$obj = new Test();
$obj->run('call method');
Test::run('call method');
$obj($obj);
?>
4.在类外也可使用const来定义常量
<?php
//PHP中定义常量通常是用这种方式
define("CONSTANT", "Hello World");
//PHP5.3新增了一种常量定义方式
const CONSTANT = 'Hello World';
?>
5.闭包、Lambda函数
<?php
//支持闭包、Lambda/Anonymous函数。闭包(Closure)函数和 lambda 函数的概念来自于函数编程领域。
//例如JavaScript 是支持闭包和 lambda 函数的最常见语言之一。//在PHP中,我们也可以通过create_function()在代码运行时创建函数。
//lambda函数
$test = function($name) {
printf("Hello %s!\r\n", $name);
};
$test('PHP');
class Test {
function __call($method, $params) {
if ($method == __CLASS__) return;
if ($this->$method instanceof Closure) {
$func = $this->$method;
return call_user_func_array($func,$params);
}
}
function run() {
$this->handler();
}
}
$test = new Test();
$test->handler = function() {
printf("Hello World!\r\n");
};
$test->run();
?>