-
Write-Once Properties
一次性写入属性,不允许被修改,有些类似js的const,但是用什么关键词还不确定
class Foo
{
<keyword> public int $a = 1;
<keyword> public string $b;
<keyword> public [array](http://www.php.net/array) $c = ["foo"];
<keyword> public object $d;
public function __construct()
{
$this->b = "foo";
}
}
$foo = new Foo();
$foo->a = 2; // EXCEPTION: property a has already been initialized
$foo->b = "bar"; // EXCEPTION: property b has already been initialized
$foo->a++; // EXCEPTION: incrementing/decrementing is forbidden
[unset](http://www.php.net/unset)($foo->c); // EXCEPTION: unsetting is forbidden
$foo->c[] = "bar"; // EXCEPTION: array values can't be modified
[next](http://www.php.net/next)($foo->c); // EXCEPTION: internal pointer of arrays can't be modified as well
$var= &$this->c; // EXCEPTION: reference isn't allowed
[key](http://www.php.net/key)($foo->c); // SUCCESS: internal pointer of arrays is possible to read
$foo->d = new Foo(); // SUCCESS: property d hasn't been initialized before
$foo->d->foo = "foo"; // SUCCESS: objects are still mutable internally</pre>
-
Userspace Operator overloading
允许用户重载操作符,像Python, C# or C++等语言都允许在类中重载操作符 (+, -, etc.)
public class Vector3()
{
// For simple cases type checking can be done by typehints.
public static function __add(Vector3 $lhs, Vector3 $rhs) {
// Do something with the values and return a non-null value
// ...
}
public static function __mul($lhs, $rhs) {
// For more complex type checking, the function can return a special const.
//...
return PHP_OPERAND_TYPES_NOT_SUPPORTED;
}
}
$a = new Vector3();
$b = new Vector3();
// Equivalent to $x = Vector3::__add($a, $b)
$x = $a + $b;
//Equivalent to $y = Vecotr3::__mul(2, $b)
$y = 3 * $b;
完整列表
operator.png
-
Server-Side Request and Response Objects
request和response面向对象类 -
Allow function calls in constant expressions
允许常量赋值为函数 -
Abstract trait method validation
在trait中定义抽象函数 -
__toArray()
新的魔术方法__toArray()
-
Comprehensions (short generators)
php的短生成器用法,类似python的推导式,但是要求总返回generator
$result = [for $list as $x if $x % 2];
$result = [for $list as $x yield $x * 2];
-
Annotations 2.0
在PHP中支持注解,类似装饰器
网友评论