php希望加入的新特性的讨论

2020-03-01  本文已影响0人  sorry510
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>

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
$result = [for $list as $x if $x % 2];
$result = [for $list as $x yield $x * 2];
上一篇 下一篇

猜你喜欢

热点阅读