欢迎来到程序员中文网!

首页 Linux Mysql C++ Python PHP JavaScript 资源下载 动态 开源推荐
我要投稿 投诉建议

PHP 8 新特性:JIT、命名参数与属性

时间:2026年08月12日 05:38:48 浏览:1

PHP 8 新特性:JIT、命名参数与属性


PHP 8 带来了诸多革命性改进,显著提升性能和开发体验。


1. JIT(即时编译)



  • 适用于 CPU 密集型任务,如图像处理、AI 计算。

  • 在 php.ini 中启用:
    opcache.jit = tracing
    opcache.jit_buffer_size = 100M

    性能提升约 10-30%。



2. 命名参数


允许按参数名传递,跳过可选参数。


function createUser(string $name, int $age = 18, string $city = '北京') {}
createUser(name: '张三', city: '上海'); // 跳过 age

3. 属性(Attributes / 注解)


替代 PHPDoc,支持运行时反射。


#[Route('/api/user', methods: ['GET'])]
function getUser() {}

// 自定义属性
#[Attribute]
class Controller {
public function __construct(public string $prefix) {}
}

4. 构造器属性提升


class User {
public function __construct(
public string $name,
public int $age,
private string $email
) {}
}

PHP 8 让这门语言更加现代化。