PHP 8突破性变化

新的PHP首要版别PHP8估计将于2020年底发布。
  它现在正处于十分活泼的开发中,所以在接下来的几个月里,开发速度和开发进程或许会有很大的改动。
  在这篇文章中,我会罗列出PHP8中会发作的一些改动:新功能、性能改善和突破性改动。
  由于PHP8是一个新的首要版别,所以代码及语法向下兼容性会更低。
  假如您一向坚持与最新版别坚持同步,那么升级应该不会太难,由于大多数突破性的更改在7.*版别中都已弃用。
  除了突破性的改动,PHP8还带来了一些不错的新特性,比方JIT编译器和uniontypes,当然还有其它更多的特性。
  新特性
  从新特性开端说起,可是PHP8仍在积极开发中,因而这个清单将跟着时间的推移而增长。
  联合类型(Uniontypes)RFC
  考虑到PHP的动态类型特性,联合类型在许多情况下都很有用。
  联合类型是两个或多个类型的调集,这些类型指示能够运用这两个类型中的任何一个。
  1
  publicfunctionfoo(Foo|Bar i n p u t ) : i n t ∣ f l o a t ;     我 怎 么 感 觉 这 个 和 C 语 言 里 的 联 合 体 有 点 类 似 。     请 注 意 , v o i d 永 远 不 能 是 联 合 类 型 的 一 部 分 , 由 于 它 表 明 “ 根 本 没 有 回 来 值 ” 。     此 外 , 能 够 运 用 ∣ N U L L 或 运 用 现 有 的 ? 。     p u b l i c f u n c t i o n f o o ( F o o ∣ n u l l input):int|float;   我怎么感觉这个和C语言里的联合体有点类似。   请注意,void永远不能是联合类型的一部分,由于它表明“根本没有回来值”。   此外,能够运用|NULL或运用现有的?。   publicfunctionfoo(Foo|null input):intfloat;  C  void  NULL  publicfunctionfoo(Foonullfoo):void;
  publicfunctionbar(?BarKaTeX parse error: Expected '}', got 'EOF' at end of input: … privateWeakMapcache;
  publicfunctiongetSomethingWithCaching(objectKaTeX parse error: Expected '}', got 'EOF' at end of input: …ct   {   returnthis->cache[ o b j ]     ? ? = obj]   ??= obj]  ??=this->computeSomethingExpensive(KaTeX parse error: Expected 'EOF', got '}' at position 9: obj);   }̲   }   能够在目标上运用…foo=newFoo();
  var_dump( f o o : : c l a s s ) ;     创 立 D a t e T i m e 目 标 的 接 口     您 现 已 能 够 运 用 D a t e T i m e : : c r e a t e F r o m I m m u t a b l e ( foo::class);   创立DateTime目标的接口   您现已能够运用DateTime::createFromImmutable( foo::class);  DateTime  DateTimecreateFromImmutable(immutableDateTime),从DateTimeImmutable目标创立DateTime目标,可是反过来很棘手。
  通过添加DateTime::createFromInterface()和DatetimeImmutable::createFromInterface(),现在有了一种将DateTime和DateTimeImmutable目标彼此转化的通用办法。
  DateTime::createFromInterface(DateTimeInterface o t h e r ) ;     D a t e T i m e I m m u t a b l e : : c r e a t e F r o m I n t e r f a c e ( D a t e T i m e I n t e r f a c e other);   DateTimeImmutable::createFromInterface(DateTimeInterface other);  DateTimeImmutable::createFromInterface(DateTimeInterfaceother);
  新的Stringable接口RFC
  Stringable接口可用于键入提示任何字符串或完成__toString()。
  此外,每逢类完成__toString()时,它都会自动在幕后完成接口,不需要手动完成它。
  classFoo
  {
  publicfunction__toString():string
  {
  return’foo’;
  }
  }
  functionbar(StringableKaTeX parse error: Undefined control sequence: \foo at position 483: …更有用的输出。   例如,在类\̲f̲o̲o̲\Bar上调用gettype(…input):int;
  }
  classUsesTrait
  {
  useTest;
  publicfunctiontest(KaTeX parse error: Expected '}', got 'EOF' at end of input: …t)   {   returninput;
  }
  }
  在运用traits并完成其笼统办法时,PHP8将执行正确的办法签名验证。
  这意味着您需要改写以下内容:
  classUsesTrait
  {
  useTest;
  publicfunctiontest(intKaTeX parse error: Expected '}', got 'EOF' at end of input: …nt   {   returninput;
  }
  }
  token_get_all()的目标接口RFC
  函数的效果是:回来值的是一个数组。
  此RFC运用PhpToken::getall()办法添加一个PhpToken类。
  此完成运用目标,而不是一般值。
  它消耗更少的内存,更简单阅读。
  变量语法调整RFC
  来自RFC:“统一变量语法RFC处理了PHP变量语法中的一些不一致问题”,这个RFC打算处理少数被疏忽的情况。
  内部函数的类型批注
  许多人都参加到为一切内部函数添加适当类型注释的工作中。
  这是一个长期存在的问题,通过在曾经版别中对PHP所做的一切更改,终究能够处理这个问题。
  这意味着内部函数和办法在反射中将具有完好的类型信息。
  统一过错类型RFC
  PHP中的用户界说函数现已抛出TypeErrors,可是内部函数没有抛出TypeErrors,而是宣布正告并回来NULL。
  从PHP8开端,内部函数的行为现已坚持一致。
  从头分类zendengine报错RFC
  许多曾经只触发正告或告诉的过错已转化为适当的过错。
  以下正告已更改。
  未界说变量:过错反常而不是告诉。
  未界说的数组索引:正告而不是告诉。
  被零除:DivisionByZeroError反常而不是正告。
  测验递加/递减非目标的特点‘%s’:过错反常而不是正告。
  企图修正非目标的特点‘%s’:过错反常而不是正告。
  测验分配非目标的特点‘%s’:过错反常而不是正告。
  从空值创立默认目标:过错反常而不是正告。
  正在测验获取非目标的特点‘%s’:正告而不是告诉。
  未界说特点:%s::KaTeX parse error: Expected 'EOF', got '#' at position 297: …正告而不是告诉。   资源ID#̲%d用作偏移量,转化为整数(%…a+ b ;     P H P 曾 经 会 这 样 解 说 它 :     e c h o ( " s u m : " . b;   PHP曾经会这样解说它:   echo("sum:". b;  PHP  echo("sum:".a)+ b ;     P H P 8 将 会 这 样 解 说 它 :     e c h o " s u m : " . ( b;   PHP8将会这样解说它:   echo"sum:".( b;  PHP8  echo"sum:".(a+ b ) ;     反 射 办 法 签 名 更 改     反 射 类 的 三 个 办 法 签 名 已 更 改 :     R e f l e c t i o n C l a s s : : n e w I n s t a n c e ( b);   反射办法签名更改   反射类的三个办法签名已更改:   ReflectionClass::newInstance( b);      ReflectionClass::newInstance(args);
  ReflectionFunction::invoke( a r g s ) ;     R e f l e c t i o n M e t h o d : : i n v o k e ( args);   ReflectionMethod::invoke( args);  ReflectionMethod::invoke(object, a r g s ) ;     现 已 成 为 :     R e f l e c t i o n C l a s s : : n e w I n s t a n c e ( . . . args);   现已成为:   ReflectionClass::newInstance(... args);    ReflectionClass::newInstance(...args);
  ReflectionFunction::invoke(… a r g s ) ;     R e f l e c t i o n M e t h o d : : i n v o k e ( args);   ReflectionMethod::invoke( args);  ReflectionMethod::invoke(object,… a r g s ) ;     升 级 攻 略 指 定 , 假 如 您 扩 展 了 这 些 类 , 而 且 依 然 希 望 同 时 支 撑 P H P 7 和 P H P 8 , 则 答 应 以 下 签 名 :     R e f l e c t i o n C l a s s : : n e w I n s t a n c e ( args);   升级攻略指定,假如您扩展了这些类,而且依然希望同时支撑PHP7和PHP8,则答应以下签名:   ReflectionClass::newInstance( args);  PHP7PHP8  ReflectionClass::newInstance(arg=null,… a r g s ) ;     R e f l e c t i o n F u n c t i o n : : i n v o k e ( args);   ReflectionFunction::invoke( args);  ReflectionFunction::invoke(arg=null,… a r g s ) ;     R e f l e c t i o n M e t h o d : : i n v o k e ( args);   ReflectionMethod::invoke( args);  ReflectionMethod::invoke(object, a r g = n u l l , . . . arg=null,... arg=null,...args);

你可能感兴趣的:(PHP 8突破性变化)