ZetCode

PHP 空安全操作符

最后修改:2025 年 2 月 15 日

在本文中,我们将展示如何在 PHP 中使用 空安全操作符。空安全操作符在 PHP 8.0 中引入,提供了一种简洁的方式来处理对象链中的 null 值。它们允许您安全地浏览可能为 null 的对象,而不会引发错误。

PHP 空安全操作符的主要特性

空安全操作符由 ?-> 语法表示。如果操作符之前的对象是 null,它们将返回 null,而不是抛出错误。

空安全操作符的基本用法

以下示例演示了如何在 PHP 中使用空安全操作符。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Address $address = null;
}

class Address {
    public string $city;
}

$user = new User();
echo $user->address?->city ?? 'Unknown';

在这个程序中,User 类有一个可空的 Address 属性。空安全操作符 ?-> 用于安全地访问 city 属性。如果 addressnull,则返回 null,并且 ?? 操作符提供一个默认值。

$ php main.php
Unknown

空安全操作符与方法调用

以下示例演示了如何将空安全操作符与方法一起使用。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public function getBio(): string {
        return "PHP Developer";
    }
}

$user = new User();
echo $user->profile?->getBio() ?? 'No bio available';

在这个程序中,Profile 类有一个 getBio 方法。空安全操作符用于安全地调用此方法。如果 profilenull,则返回默认值。

$ php main.php
No bio available

链式调用空安全操作符

以下示例演示了如何链式调用空安全操作符。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public ?Address $address = null;
}

class Address {
    public string $city = "New York";
}

$user = new User();
echo $user->profile?->address?->city ?? 'Unknown';

在这个程序中,空安全操作符用于安全地浏览多个级别的可能为 null 的对象。如果链中的任何对象为 null,结果将是 null

$ php main.php
Unknown

空安全操作符与数组

以下示例演示了如何将空安全操作符与数组一起使用。

main.php
<?php

declare(strict_types=1);

class User {
    public ?array $preferences = null;
}

$user = new User();
echo $user->preferences?['theme'] ?? 'Default theme';

在这个程序中,空安全操作符用于安全地访问数组元素。如果 preferencesnull,则返回默认值。

$ php main.php
Default theme

空安全操作符与静态方法

以下示例演示了如何将空安全操作符与静态方法一起使用。

main.php
<?php

declare(strict_types=1);

class User {
    public static ?string $name = null;
}

echo User::$name?->toLowerCase() ?? 'Anonymous';

在这个程序中,空安全操作符用于安全地调用静态方法。如果 $namenull,则返回默认值。

$ php main.php
Anonymous

空安全操作符与 null 合并运算符

以下示例演示了如何将空安全操作符与 null 合并运算符结合使用。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public ?string $bio = null;
}

$user = new User();
echo $user->profile?->bio ?? 'No bio available';

在这个程序中,空安全操作符用于安全地访问 bio 属性。如果 profilebionull,则返回默认值。

$ php main.php
No bio available

空安全操作符与函数调用

以下示例演示了如何在函数调用中使用空安全操作符。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public function getBio(): string {
        return "PHP Developer";
    }
}

function displayBio(?User $user): void {
    echo $user?->profile?->getBio() ?? 'No bio available';
}

$user = new User();
displayBio($user);

在这个程序中,空安全操作符在函数内部用于安全地访问 getBio 方法。如果 userprofilenull,则返回默认值。

$ php main.php
No bio available

空安全操作符与嵌套对象

以下示例演示了如何将空安全操作符与深度嵌套的对象一起使用。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public ?Address $address = null;
}

class Address {
    public ?string $city = "New York";
}

$user = new User();
echo $user->profile?->address?->city ?? 'Unknown';

在这个程序中,空安全操作符用于安全地浏览多个级别的可能为 null 的对象。如果链中的任何对象为 null,结果将是 null

$ php main.php
Unknown

空安全操作符与可选参数

以下示例演示了如何将空安全操作符与可选参数一起使用。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public function getBio(?string $default = null): string {
        return $default ?? "PHP Developer";
    }
}

$user = new User();
echo $user->profile?->getBio('No bio available') ?? 'Anonymous';

在这个程序中,空安全操作符用于安全地调用带有可选参数的方法。如果 profilenull,则返回默认值。

$ php main.php
Anonymous

空安全操作符与对象初始化

以下示例展示了空安全操作符与已初始化对象一起使用。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
}

class Profile {
    public string $name = "John Doe";
}

$user = new User();
$user->profile = new Profile();
echo $user->profile?->name ?? 'Unknown';

在这个程序中,空安全操作符在初始化对象后访问一个属性。如果 profilenull,则应用默认值。

$ php main.php
John Doe

空安全操作符与多链

以下示例在一个表达式中使用多个空安全链。

main.php
<?php

declare(strict_types=1);

class User {
    public ?Profile $profile = null;
    public ?Settings $settings = null;
}

class Profile {
    public string $role = "Developer";
}

class Settings {
    public string $mode = "Dark";
}

$user = new User();
echo $user->profile?->role . " " . $user->settings?->mode ?? 'Unknown';

在这个程序中,两个空安全链获取属性。如果其中任何一个为 null,则对整个表达式使用默认值。

$ php main.php
Unknown

来源

PHP 空安全操作符 - 文档

在本文中,我们展示了如何在 PHP 中使用 空安全操作符,以便安全地浏览可能为 null 的对象。 空安全操作符是编写更清晰、更健壮代码的强大工具。

作者

我的名字是 Jan Bodnar,我是一位充满激情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。到目前为止,我撰写了超过 1400 篇文章和 8 本电子书。我拥有超过十年的编程教学经验。

列出 所有 PHP 教程