PHP 8 in 8 code blocks

栏目: IT技术 · 发布时间: 4年前

内容简介:PHP 8 brings lots of new features, in this list we'll look at the most outstanding ones. If you want a full list and background information, you can read about all thingsnew in PHP 8.Attributes — aka annotations — you can about them in depth inthis post.Un

PHP 8 brings lots of new features, in this list we'll look at the most outstanding ones. If you want a full list and background information, you can read about all thingsnew in PHP 8.

use \Support\Attributes\ListensTo;

class ProductSubscriber
{
    <<ListensTo(ProductCreated::class)>>
    public function onProductCreated(ProductCreated $event) { /* … */ }

    <<ListensTo(ProductDeleted::class)>>
    public function onProductDeleted(ProductDeleted $event) { /* … */ }
}

Attributes — aka annotations — you can about them in depth inthis post.

public function foo(Foo|Bar $input): int|float;

public function bar(mixed $input): mixed;

Union types allows for type hinting several types. There's also a new mixed type which representsseveral types at once.

interface Foo
{
    public function bar(): static;
}

The static return type is built-in.

[JIT]
opcache.jit=5

The just-in-time compiler for PHP.

$triggerError = fn() => throw new MyError();

$foo = $bar['offset'] ?? throw new OffsetDoesNotExist('offset');

throw can be used in expressions.

try {
    // Something goes wrong
} catch (MySpecialException) {
    Log::error("Something went wrong");
}

Non-capturing catches: no need to specify an exception variable if you don't need it.

public function(
    string $parameterA,
    int $parameterB,
    Foo $objectfoo,
) {
    // …
}

Trailing commas are allowed in parameter lists

str_contains('string with lots of words', 'words');

str_starts_with('haystack', 'hay');

str_ends_with('haystack', 'stack');

New string functions.

Let's not fool ourselves: 8 code blocks isn't enough to summarise all great new things in PHP 8. So let's just add a few more.

function bar(Stringable $stringable) { /* … */ }

A new Stringable interface .

$object::class

Call ::class directly on objects.

There's even more. If you want a full list, you can find iton this blog.

What feature are you looking forward to the most? Let me know on Twitter or viaemail.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

神一样的产品经理

神一样的产品经理

闫荣 / 电子工业出版社 / 2012-6-1 / 79.00元

这是一本系统阐述移动与互联网产品从无到有、从有到优的产品经理实践案例著作。《神一样的产品经理:基于移动与互联网产品实践》贯穿着“人如产品,产品如人”、“产品的根基和源泉来自现实生活”的写作理念,表达了产品的成功需要神一样的产品经理管理的观点。 《神一样的产品经理:基于移动与互联网产品实践》由浅入深、循序渐进地阐述了产品经理、产品需求、用户体验、项目管理、产品运营和产品团队管理的内容,理论与实......一起来看看 《神一样的产品经理》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具