美文网首页
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希望加入的新特性的讨论

    Write-Once Properties一次性写入属性,不允许被修改,有些类似js的const,但是用什么关键词...

  • php7.1加密解密 openssl_encrypt 替代

    1 概况 php7.1发布后新特性吸引了不少PHPer,大家都在讨论新特性带来的好处与便利。但是从php7.0 升...

  • 12月13日下午:模板分离和公共文件导入

    use是php5.3以后加入的新特性,相当于require和includeuse Think\Controllle...

  • php5.5 新特性

    php5.5新特性

  • 生成器

    PHP Manual手册对于生成器的概述:PHP Manual Generator PHP Manual手册新特性...

  • php新特性

    php5.6新特性 参考 使用表达式定义常量,使用const定义常量数组 使用 ... 运算符定义变长参数函数 使...

  • PHP新特性

    PHP5.5新特性 字符串直接表达式 直接通过中括号获取字符串中的单个字符。 foreach和list() for...

  • PHP 新特性

    命名空间 namespace 其作用是按照一种虚拟的层次结构组织PHP代码,现代PHP组件和框架都放在各自全局唯一...

  • PHP5.5 新特性介绍

    最近PHP5.5已经发布,引入了一些新特性。本文将介绍这些特性,并讨论它们可以给开发者带来哪些好处。 生成器(Ge...

  • PHP全栈学习笔记2

    php概述 什么是php,PHP语言的优势,PHP5的新特性,PHP的发展趋势,PHP的应用领域。 PHP是超文本...

网友评论

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

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