跳到主要内容

PHP Version

Versiondateeol
PHP 8.4
PHP 8.32023-11-23
PHP 8.22022-12-08
PHP 8.12021-11-25
PHP 8.02020-11-26
PHP 7.42019-11-282022-11-28

PHP 8.4

PHP 8.3

  • lint - php -l
  • json_validate
  • gc_status
  • class_alias
<?php
class Test {
// Typed Class Constants
const string TEST_CONSTANT = 'test';
}

$constName = 'TEST_CONSTANT';

// Dynamic class constant and Enum member fetch support
echo MyClass::{$constName};

PHP 8.2

  • readonly 类
<?php
readonly class BlogData
{
public string $title;

public Status $status;

public function __construct(string $title, Status $status)
{
$this->title = $title;
$this->status = $status;
}
}
  • Disjunctive Normal Form (DNF) 类型
<?php
class Foo {
public function bar((A&B)|null $entity) {
return $entity;
}
}

PHP 8.1

  • fiber
    • amphp
  • enum
<?php
enum Status
{
case Draft;
case Published;
case Archived;
}
  • readonly 属性
<?php
class BlogData
{
public readonly Status $status;

public function __construct(Status $status)
{
$this->status = $status;
}
}
  • first class callable
<?php
// 类似于 获取函数引用
$foo = $this->foo(...);
$fn = strlen(...);
  • Intersection Types
<?php
function count_and_iterate(Iterator&Countable $value) {
foreach ($value as $val) {
echo $val;
}

count($value);
}
  • never 类型 - 不返回
  • array spread
<?php
$arrayA = ['a' => 1];
$arrayB = ['b' => 2];

// array_merge
$result = ['a' => 0, ...$arrayA, ...$arrayB];

PHP 8.0

  • JIT
  • 语法
    • match
    • static, mixed
    • nullsafe $user->getBirthday()?->diffForHumans()
  • stdlib
    • WeakMap
    • DateTime
    • Stringable
  • 命名参数
  • Attributes
  • Union 类型 int|float
  • Nullsafe operator - ?->
  • 0 == 'foobar'
    • true -> false
<?php
// 类似 python 语法
htmlspecialchars($string, double_encode: false);

// 类似 java annotation、js decorator
class PostsController
{
#[Route("/api/posts/{id}", methods: ["GET"])]
public function get($id) { /* ... */ }
}

// 构造函数属性
class Point {
public function __construct(
public float $x = 0.0,
public float $y = 0.0,
public float $z = 0.0,
) {}
}

// Match 表达式
// 类似 go 的 select
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};

PHP 7.4

PHP 7.3

  • Array Destructuring
    • 支持引用

PHP 7.2

  • Object typehint

PHP 7.1

  • Nullable
  • void 返回类型
  • Iterable 类型
  • list [], 支持 key

PHP 7.0

  • Zend Engine
  • Anonymous Classes
  • ?? - null coalescing operator

PHP 5.6