C'est La Vie

    人生无彩排,每一天都是现场直播!

    简单介绍一下PHP 5.4的新特性

    1. 实例化时访问类成员 class Human{ function __construct($name){ […]

    1. 实例化时访问类成员

    class Human{
    	function __construct($name){
    		$this->name = $name;
    	}
    	public function hello(){
    		return "Hi " . $this->name;
    	}
    }
    // old style
    $human = new Human("ixcv");
    echo $human->hello();
    
    // new cool style
    echo (new Human("ixcv"))->hello();

    2. 短数组定义语法

    $a = [1, 2, 3];
    print_r($a);

    3. 支持 Class::{expr}() 语法

    foreach ([new Human("ixcv"), new Human("Peter")] as $human)
    	echo $human->{'hello'}();
    }

    4. 通过数组间接调用方法

    $f = [new Human("ixcv"), 'hello'];
    echo $f();

    5. Callable typehint

    function hi(callable $f) {
    	$f();
    }
    hi([new Human("ixcv"), 'hello']);

    6. Traits

    trait FlyMutant {
    	public function fly() {
    		return 'I can fly!';
    	}
    }
    
    class Mutant extends Human {
    	use FlyMutant;
    }
    $mutant = new Mutant("Storm");
    echo $mutant->fly();

    7. 支持数组提领(Array dereferencing support)

    function data() {
    	return ['name' => 'ixcv', 'surname' => 'Ayuso'];
    }
    echo data()['name'];

    发表回复

    您的电子邮箱地址不会被公开。