美文网首页
php希望加入的新特性的讨论

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

作者: sorry510 | 来源:发表于2020-03-01 18:03 被阅读0次
    • 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>
    
    
    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];
    

    相关文章

      网友评论

          本文标题:php希望加入的新特性的讨论

          本文链接:https://www.haomeiwen.com/subject/rsbckhtx.html