PHP 空安全操作符
最后修改:2025 年 2 月 15 日
在本文中,我们将展示如何在 PHP 中使用 空安全操作符。空安全操作符在 PHP 8.0 中引入,提供了一种简洁的方式来处理对象链中的 null 值。它们允许您安全地浏览可能为 null 的对象,而不会引发错误。
PHP 空安全操作符的主要特性
- 安全导航:空安全操作符在访问 null 对象的属性或方法时可防止错误。
- 简洁的语法:它们减少了对冗长的 null 检查的需求。
- 提高可读性:代码变得更清晰,更容易理解。
- 链式调用支持:空安全操作符可用于链式方法或属性调用。
- 错误预防:它们有助于避免由 null 引用引起的运行时错误。
空安全操作符由 ?-> 语法表示。如果操作符之前的对象是 null,它们将返回 null,而不是抛出错误。
空安全操作符的基本用法
以下示例演示了如何在 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 属性。如果 address 为 null,则返回 null,并且 ?? 操作符提供一个默认值。
$ php main.php Unknown
空安全操作符与方法调用
以下示例演示了如何将空安全操作符与方法一起使用。
<?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 方法。空安全操作符用于安全地调用此方法。如果 profile 为 null,则返回默认值。
$ php main.php No bio available
链式调用空安全操作符
以下示例演示了如何链式调用空安全操作符。
<?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
空安全操作符与数组
以下示例演示了如何将空安全操作符与数组一起使用。
<?php
declare(strict_types=1);
class User {
public ?array $preferences = null;
}
$user = new User();
echo $user->preferences?['theme'] ?? 'Default theme';
在这个程序中,空安全操作符用于安全地访问数组元素。如果 preferences 为 null,则返回默认值。
$ php main.php Default theme
空安全操作符与静态方法
以下示例演示了如何将空安全操作符与静态方法一起使用。
<?php
declare(strict_types=1);
class User {
public static ?string $name = null;
}
echo User::$name?->toLowerCase() ?? 'Anonymous';
在这个程序中,空安全操作符用于安全地调用静态方法。如果 $name 为 null,则返回默认值。
$ php main.php Anonymous
空安全操作符与 null 合并运算符
以下示例演示了如何将空安全操作符与 null 合并运算符结合使用。
<?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 属性。如果 profile 或 bio 为 null,则返回默认值。
$ php main.php No bio available
空安全操作符与函数调用
以下示例演示了如何在函数调用中使用空安全操作符。
<?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 方法。如果 user 或 profile 为 null,则返回默认值。
$ php main.php No bio available
空安全操作符与嵌套对象
以下示例演示了如何将空安全操作符与深度嵌套的对象一起使用。
<?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
空安全操作符与可选参数
以下示例演示了如何将空安全操作符与可选参数一起使用。
<?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';
在这个程序中,空安全操作符用于安全地调用带有可选参数的方法。如果 profile 为 null,则返回默认值。
$ php main.php Anonymous
空安全操作符与对象初始化
以下示例展示了空安全操作符与已初始化对象一起使用。
<?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';
在这个程序中,空安全操作符在初始化对象后访问一个属性。如果 profile 为 null,则应用默认值。
$ php main.php John Doe
空安全操作符与多链
以下示例在一个表达式中使用多个空安全链。
<?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 中使用 空安全操作符,以便安全地浏览可能为 null 的对象。 空安全操作符是编写更清晰、更健壮代码的强大工具。
作者
列出 所有 PHP 教程。