PHP 联合类型
最后修改于 2025 年 3 月 10 日
PHP 联合类型允许变量、参数或返回类型接受多种类型。在 PHP 8.0 中引入,联合类型在类型声明中提供了更大的灵活性和表达能力。本教程通过实际例子涵盖了联合类型的基础和高级用法。
联合类型对于可以处理多种输入类型或根据条件返回不同类型的函数特别有用。
基本联合类型
此示例演示如何声明一个可以接受整数或字符串的参数。
basic_union.php
<?php
declare(strict_types=1);
function printValue(int|string $value): void {
echo $value;
}
printValue(42); // Output: 42
printValue("Hello"); // Output: Hello
int|string 语法指定 $value 参数可以是整数或字符串。
联合类型在返回值中
此示例展示了如何声明一个可以返回整数或浮点数的函数。
return_union.php
<?php
declare(strict_types=1);
function divide(int $a, int $b): int|float {
if ($b === 0) {
return 0.0; // Return float for division by zero
}
return $a / $b;
}
echo divide(10, 2); // Output: 5
echo divide(10, 0); // Output: 0.0
int|float 返回类型允许函数返回整数或浮点数。
联合类型与 Null
此示例演示了如何将联合类型与 null 一起使用,以指示参数或返回值可以为空。
nullable_union.php
<?php
declare(strict_types=1);
function findUser(int $id): string|null {
if ($id === 1) {
return "John Doe";
}
return null;
}
echo findUser(1) ?? "User not found"; // Output: John Doe
echo findUser(2) ?? "User not found"; // Output: User not found
string|null 返回类型允许函数返回字符串或 null。
类属性中的联合类型
此示例展示了如何在类属性中使用联合类型。
class_property_union.php
<?php
declare(strict_types=1);
class User {
public int|string $id;
public function __construct(int|string $id) {
$this->id = $id;
}
}
$user1 = new User(1);
$user2 = new User("abc123");
echo $user1->id; // Output: 1
echo $user2->id; // Output: abc123
int|string 类型声明允许 $id 属性存储整数或字符串。
高级:联合类型与数组
此示例演示了如何将联合类型与数组一起使用。
array_union.php
<?php
declare(strict_types=1);
function processArray(array|string $input): void {
if (is_array($input)) {
echo "Processing array: " . implode(", ", $input);
} else {
echo "Processing string: " . $input;
}
}
processArray([1, 2, 3]); // Output: Processing array: 1, 2, 3
processArray("Hello"); // Output: Processing string: Hello
array|string 类型声明允许 $input 参数是数组或字符串。
联合类型与对象
此示例展示了如何将联合类型与不同的对象类型一起使用。
object_union.php
<?php
declare(strict_types=1);
class Cat {
public function speak(): string {
return "Meow";
}
}
class Dog {
public function speak(): string {
return "Woof";
}
}
function makeAnimalSpeak(Cat|Dog $animal): string {
return $animal->speak();
}
$cat = new Cat();
$dog = new Dog();
echo makeAnimalSpeak($cat); // Output: Meow
echo makeAnimalSpeak($dog); // Output: Woof
Cat|Dog 类型声明允许参数接受 Cat 或 Dog 对象。
联合类型与混合数值
此示例演示了如何处理多种数值类型。
numeric_union.php
<?php
declare(strict_types=1);
function calculateSquare(int|float $number): float {
return $number * $number;
}
echo calculateSquare(5); // Output: 25
echo calculateSquare(2.5); // Output: 6.25
int|float 类型允许接受整数和浮点数输入,但始终返回一个浮点数。
联合类型与布尔值或整数
此示例展示了如何处理配置设置的布尔值或整数输入。
bool_int_union.php
<?php
declare(strict_types=1);
function setConfig(bool|int $value): string {
if (is_bool($value)) {
return $value ? "Enabled" : "Disabled";
}
return "Level: " . $value;
}
echo setConfig(true); // Output: Enabled
echo setConfig(0); // Output: Level: 0
echo setConfig(42); // Output: Level: 42
bool|int 类型允许接受布尔值和整数值。
联合类型的最佳实践
- 谨慎使用: 仅在必要时使用联合类型,以避免过度复杂化代码。
- 记录行为: 清楚地记录具有联合类型的函数,以解释预期的输入和输出。
- 处理所有情况: 确保您的代码处理联合中的所有可能类型,以避免运行时错误。
- 与类型检查结合使用: 使用
is_int、is_string等来处理函数中的不同类型。
来源
在本文中,我们探讨了使用 PHP 联合类型的各种示例,包括基本用法、返回类型、可空类型以及与数组的高级场景。
作者
列出 所有 PHP 教程。