/

PHP Migrating 8.3 to 8.4

PHP 8.4 contains many new features, such as property hooks, asymmetric visibility, an updated DOM API.

New Features 8.4

// Property Hooks & Asymmetric Visibility
class User {
private bool $isModified = false;
public function __construct(public private(set) string $first, private string $last)
{
}
public string $fullName {
get => $this->first . " " . $this->last;
set {
[$this->first, $this->last] = explode(' ', $value, 2);
$this->isModified = true;
}
}
}
$user = new User("Ming", "Li");
echo $user->fullName; // Ming Li
$user->fullName = "Hong Xiao";
echo $user->fullName; // Hong Xiao

// Asymmetric Visibility
echo $user->first; // Hong
$user->first = "hua";
// Fatal error: Uncaught Error: Cannot modify private(set) property User::$first
// Object API for BCMath
use BcMath\Number;
$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2;
echo $result; // '2.12345'
var_dump($num1 > $num2); // false

// PHP < 8.4
$num1 = '0.12345';
$num2 = 2;
$result = bcadd($num1, $num2, 5);
echo $result; // '2.12345'
var_dump(bccomp($num1, $num2) > 0); // false

New Functions 8.4

// New array_*() functions
// array_find(array $array, callable $callback): mixed
$animal = array_find(
['dog', 'cat', 'cow', 'duck', 'goose'],
static fn (string $value): bool => str_starts_with($value, 'c'),
);
var_dump($animal); // string(3) "cat"

// array_find_key(array $array, callable $callback): mixed

// Checks if at least one array element satisfies a callback function
// array_any(array $array, callable $callback): bool
array_any($[1, 2, 3, 4], fn($n) => $n > 3); // true

// Checks if all array elements satisfy a callback function
// array_all(array $array, callable $callback): bool

Deprecated Features 8.4

// 隐式可空类型:需显式声明 ?string
// ❌ PHP8.4 Deprecated
function save(Book $book = null) {}
// ✅ Fix: 显式声明可空
function save(?Book $book = null) {}
// 不安全哈希函数:md5()/sha1() 被标记为不安全

– EOF –