蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

在 PHP 8.4 中验证 PHP 7+ 核心语法、安全随机、过滤反序列化与 Throwable

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-php7-new-features.php
explore-php7-new-features.php136 行
<?php
declare(strict_types=1);

interface MessageLogger
{
    public function log(string $msg): string;
}

class SecretBox
{
    private string $token = 'php7-closure-ok';
}

function sumInts(int ...$nums): int
{
    return array_sum($nums);
}

$cases = [];

// Case 1: Scalar parameter/return types, null coalescing (??), spaceship operator (<=>)
$input = ['role' => null];
$role = $input['role'] ?? $input['fallback'] ?? 'guest';
$actual1 = [
    'sum_ints' => sumInts(2, 3, 5),
    'null_coalesce' => $role,
    'spaceship_lt' => 1 <=> 2,
    'spaceship_eq' => 2 <=> 2,
    'spaceship_gt' => 3 <=> 2,
];
$expected1 = [
    'sum_ints' => 10,
    'null_coalesce' => 'guest',
    'spaceship_lt' => -1,
    'spaceship_eq' => 0,
    'spaceship_gt' => 1,
];
$cases[] = [
    'name' => 'scalar_types_null_coalesce_spaceship',
    'passed' => $actual1 === $expected1,
    'expected' => $expected1,
    'actual' => $actual1,
];

// Case 2: define() array constants, anonymous class, Closure::call()
define('DEMO_DB_DRIVERS', ['mysql', 'sqlite', 'pgsql']);
$logger = new class implements MessageLogger {
    public function log(string $msg): string
    {
        return '[ANON] ' . $msg;
    }
};
$readSecret = function (): string {
    return $this->token;
};
$actual2 = [
    'const_array_item' => DEMO_DB_DRIVERS[1],
    'anon_implements' => $logger instanceof MessageLogger,
    'anon_output' => $logger->log('ready'),
    'closure_call' => $readSecret->call(new SecretBox()),
];
$expected2 = [
    'const_array_item' => 'sqlite',
    'anon_implements' => true,
    'anon_output' => '[ANON] ready',
    'closure_call' => 'php7-closure-ok',
];
$cases[] = [
    'name' => 'array_constant_anon_class_closure_call',
    'passed' => $actual2 === $expected2,
    'expected' => $expected2,
    'actual' => $actual2,
];

// Case 3: intdiv(), filtered unserialize(), CSPRNG random_bytes()/random_int()
$serialized = serialize(new SecretBox());
$blockedObj = unserialize($serialized, ['allowed_classes' => false]);
$randBytes = random_bytes(8);
$randInt = random_int(100, 200);
$actual3 = [
    'intdiv_10_3' => intdiv(10, 3),
    'intdiv_neg7_2' => intdiv(-7, 2),
    'unserialize_blocked' => $blockedObj instanceof __PHP_Incomplete_Class,
    'csprng_bytes_len' => strlen($randBytes),
    'csprng_int_in_range' => $randInt >= 100 && $randInt <= 200,
];
$expected3 = [
    'intdiv_10_3' => 3,
    'intdiv_neg7_2' => -3,
    'unserialize_blocked' => true,
    'csprng_bytes_len' => 8,
    'csprng_int_in_range' => true,
];
$cases[] = [
    'name' => 'intdiv_filtered_unserialize_csprng',
    'passed' => $actual3 === $expected3,
    'expected' => $expected3,
    'actual' => $actual3,
];

// Case 4: PHP 7+ Throwable/Error handling and removed legacy ext/mysql check
$caughtErrorClass = '';
try {
    intdiv(10, 0);
} catch (Throwable $e) {
    $caughtErrorClass = get_class($e);
}
$typeErrorCaught = false;
try {
    /** @noinspection PhpStrictTypeCheckingInspection */
    sumInts('not-an-int');
} catch (TypeError $e) {
    $typeErrorCaught = true;
}

$actual4 = [
    'caught_div_zero' => $caughtErrorClass,
    'caught_type_error' => $typeErrorCaught,
    'legacy_mysql_removed' => !function_exists('mysql_connect'),
    'php_major_ge_7' => PHP_MAJOR_VERSION >= 7,
];
$expected4 = [
    'caught_div_zero' => 'DivisionByZeroError',
    'caught_type_error' => true,
    'legacy_mysql_removed' => true,
    'php_major_ge_7' => true,
];
$cases[] = [
    'name' => 'php7_throwable_error_and_removed_ext',
    'passed' => $actual4 === $expected4,
    'expected' => $expected4,
    'actual' => $actual4,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-php7-new-features-lab/explore-php7-new-features.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-php7-new-features.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1281.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习