PHP8中都在增加什么有趣的新特性?
资讯
0
1989
2020-07-31 12:04:22
<blockquote>
<p><span style="font-size:14px">导</span><span style="font-size:14px">读:</span><span style="font-size:14px">PHP8会让我们写代码的方式焕然一新。</span></p>
</blockquote>
<p> </p>
<div class="active aw-upload-img-list"><a href="https://www.21cto.com/uploads/article/20200731/4c96242f720a89e861f23df7fa698835.png" rel="lightbox" target="_blank"><img alt="php-logo-svg-2cef10039f7c9cd395ca32ced2508bcf.png" class="img-polaroid" src="https://www.21cto.com/uploads/article/20200731/4c96242f720a89e861f23df7fa698835.png" title="php-logo-svg-2cef10039f7c9cd395ca32ced2508bcf.png" /></a></div>
<p><br />
<br />
<span style="font-size:15px">各位广大干部农工同志们,PHP8的正式版本将于2020年11月26日正式发布,届时它的全部闪光特性都将稳定地并全部可用。</span><br />
<br />
<span style="font-size:15px">本文撰写在PHP8 时仍处 Alpha阶段,这表示后面仍然会有功能更新。以下是我总结的新功能概要,来瞧瞧这些功能对开发者来说意味着啥。</span><br />
<br />
<strong><span style="font-size:15px">一 属性</span></strong><br />
<br />
<span style="font-size:15px">毫无疑问,属性是PHP语言的最大补充。</span><br />
<br />
<span style="font-size:15px">属性有助于将元数据添加到函数、参数、类、方法、常量、属性等。这些东西在之前是通过docblock模拟的,然后通过其它地方来解析。</span><br />
<br />
<span style="font-size:15px">如今,属性可是PHP中的一等公民,并且可以通过编程进行访问。</span><br />
<br />
<span style="font-size:15px">请看如下代码:</span></p>
<p> </p>
<pre>
<code class="language-php">@@Route(Http::POST, '/store/123/item')
class ItemCreateHandler
{
public function __invoke() {
// ...
}
}</code></pre>
<p><br />
<span style="font-size:15px">目前,PHP的属性之概念仍然不断进化,虽然已经定下从<<br />
<br />
<span style="font-size:15px">他说本人更喜欢Rust的方法: #[FooAttribute]</span><br />
</span></p>
<div class="active aw-upload-img-list"><span style="font-size:15px"><a href="https://www.21cto.com/uploads/article/20200731/19a593274e6a8f744c6ab4410c22e7f8.jpg" rel="lightbox" target="_blank"><img alt="1596158887124.jpg" class="img-polaroid" src="https://www.21cto.com/uploads/article/20200731/19a593274e6a8f744c6ab4410c22e7f8.jpg" title="1596158887124.jpg" /></a> </span></div>
<p><br />
<br />
<span style="font-size:15px">Derick说的的确如此,社区也表示认可。这表明属性在功能冻结期间仍然有可能会更新RFC。不论哪种方式,我们都不能否认属性是PHP开发者们欢迎的功能。</span><br />
<br />
<strong><span style="font-size:15px">面向对象更新</span></strong><br />
<br />
<span style="font-size:15px">现在我们经常写构造方法,代码是这样式儿写的:</span></p>
<pre>
<code class="language-php">class Response {
private int $code;
private string $body;
private array $headers;
public function __construct(int $code, string $body, array $headers) {
$this->code = $code;
$this->body = $body;
$this->headers = $headers;
}
}</code></pre>
<p><br />
<br />
<span style="font-size:15px">人们也习以为常,习惯了如此方式。而在PHP8中,可以这样简写:</span></p>
<pre>
<code class="language-php">class Response {
public function __construct(
private int $code,
private string $body,
private array $headers,
) {}
}</code></pre>
<p><br />
<span style="font-size:15px">是不是变得更整齐了?</span><br />
<br />
<strong><span style="font-size:15px">match表达式</span></strong><br />
<br />
<span style="font-size:15px">你可能以前也写过很复杂的Switch语句和表达式,比如下面这样的代码:</span></p>
<pre>
<code class="language-php">switch ($x) {
case 1:
$y = 3;
break;
case 2:
$y = 4
break;
case 3: case 4:
$y = 5;
break;
...
default:
throw new \RuntimeException('Not happening, bud');
}
</code></pre>
<p><br />
<span style="font-size:15px">还是有点太长了。现在,人们可以用更简短的match表达式:、</span></p>
<pre>
<code class="language-php">$y = match ($x) {
1 => 3,
2 => 4,
3, 4 => 5,
...
default => throw new \RuntimeException('Not happening, bud'),
}</code></pre>
<p><br />
<span style="font-size:15px">match表达式可能是受到ES6的启发,让每个匹配的案例仅包含一个表达式,同时也包含一个隐式的break表达式。</span><br />
<br />
<strong><span style="font-size:15px">联合类型</span></strong><br />
<br />
<span style="font-size:15px">PHP是一个动态类型语言。使用联合类型将可为参数或返回值类型指定2个或更多可接受的类型。</span><br />
<br />
<span style="font-size:15px">目前PHP支持两种特殊联合类型,Type/null以及array/Traversable。</span><br />
<br />
<span style="font-size:15px">现在我们来代替phpdoc处理类型,使用如下代码:</span></p>
<pre>
<code class="language-php">class Number {
private int|float $number;
public function setNumber(int|float $number): void {
$this->number = $number;
}
public function getNumber(): int|float {
return $this->number;
}
}</code></pre>
<p><br />
<br />
<span style="font-size:15px">也可以使用null:</span></p>
<pre>
<code class="language-php">public function handle(Product|null $product): int
// equals
public function handle(?Product $product): int</code></pre>
<p><br />
<strong><span style="font-size:15px">命名参数</span></strong><br />
<br />
<span style="font-size:15px">命名参数允许基于参数名称,而不再是基于参数位置来将值传递给参数。如下代码示例:</span></p>
<pre>
<code class="language-php">// Positional arguments
json_encode($data, 0, 512);
// Named arguments
json_encode(value: $data, options: 0, depth: 512);</code></pre>
<p><br />
<span style="font-size:15px">开发者可以选择自己喜欢的任一种。</span><br />
<br />
<strong><span style="font-size:15px">小结</span></strong><br />
<br />
<span style="font-size:15px">直到今,PHP8 仍然在更新中,社区也欢迎更多的改良意见。可以预见的是,新版本的PHP将会让我们写PHP代码的方式焕然一新!</span></p>
<p>
<p>
<p>
<blockquote>
<p><span style="font-size:14px">作者:洛逸</span><br />
<span style="font-size:14px">来源:21CTO</span></p>
</blockquote>
</p>
</p>
</p>
<p> </p>
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。