ZetCode

PHP 匿名类

最后修改于 2025 年 3 月 11 日

PHP 匿名类允许您内联定义和实例化一个类,而无需为其命名。 它们对于创建简单的、一次性对象或用于实现接口和即时扩展类非常有用。 本教程涵盖了匿名类的基本和高级用法,并附有实际示例。

匿名类在您只需要一个类的单次使用,并且不想用不必要的类定义来混乱代码的情况下特别有用。

基本匿名类

此示例演示了如何创建和使用基本的匿名类。

basic_anonymous.php
<?php declare(strict_types=1);

$greeting = new class {
    public function sayHello(): string {
        return "Hello, World!";
    }
};

echo $greeting->sayHello();  // Output: Hello, World!

匿名类使用 new class 语法实例化并分配给变量 $greeting。然后调用 sayHello 方法来显示消息。

实现一个接口

此示例展示了如何创建实现接口的匿名类。

implement_interface.php
<?php declare(strict_types=1);

interface Greeting {
    public function sayHello(): string;
}

$greeting = new class implements Greeting {
    public function sayHello(): string {
        return "Hello, World!";
    }
};

echo $greeting->sayHello();  // Output: Hello, World!

匿名类实现了 Greeting 接口,并为 sayHello 方法提供了实现。

扩展一个类

此示例演示了如何创建一个扩展现有类的匿名类。

extend_class.php
<?php declare(strict_types=1);

class BaseGreeting {
    public function sayHello(): string {
        return "Hello, Base!";
    }
}

$greeting = new class extends BaseGreeting {
    public function sayHello(): string {
        return "Hello, Anonymous!";
    }
};

echo $greeting->sayHello();  // Output: Hello, Anonymous!

匿名类扩展了 BaseGreeting 类并重写了 sayHello 方法。

使用构造函数参数

此示例展示了如何将参数传递给匿名类的构造函数。

constructor_arguments.php
<?php declare(strict_types=1);

$greeting = new class("World") {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function sayHello(): string {
        return "Hello, " . $this->name . "!";
    }
};

echo $greeting->sayHello();  // Output: Hello, World!

匿名类接受一个构造函数参数,该参数用于自定义 sayHello 方法的输出。

使用 Traits

此示例演示了如何在匿名类中使用 Traits。

use_traits.php
<?php declare(strict_types=1);

trait GreetingTrait {
    public function sayHello(): string {
        return "Hello, Trait!";
    }
}

$greeting = new class {
    use GreetingTrait;
};

echo $greeting->sayHello();  // Output: Hello, Trait!

匿名类使用 GreetingTrait trait 提供 sayHello 方法。

带有属性的匿名类

此示例展示了一个带有类型化属性的匿名类。

properties.php
<?php declare(strict_types=1);

$person = new class {
    public string $name = "Anonymous";
    public int $age = 30;

    public function getInfo(): string {
        return "Name: $this->name, Age: $this->age";
    }
};

echo $person->getInfo();  // Output: Name: Anonymous, Age: 30

匿名类定义了类型化属性和一个用于显示它们的方法。

匿名类作为回调

此示例使用匿名类作为回调对象。

callback.php
<?php declare(strict_types=1);

function processCallback(object $callback): string {
    return $callback->execute();
}

$result = processCallback(new class {
    public function execute(): string {
        return "Callback executed!";
    }
});

echo $result;  // Output: Callback executed!

匿名类作为对象传递给期望回调的函数。

具有多个方法的匿名类

此示例展示了一个具有多个类型化方法的匿名类。

multiple_methods.php
<?php declare(strict_types=1);

$calculator = new class {
    public function add(int $a, int $b): int {
        return $a + $b;
    }

    public function multiply(int $a, int $b): int {
        return $a * $b;
    }
};

echo $calculator->add(5, 3);      // Output: 8
echo "\n";
echo $calculator->multiply(4, 2); // Output: 8

匿名类提供了具有严格类型用于数学运算的多个方法。

匿名类的最佳实践

来源

PHP 匿名类文档

在本文中,我们探讨了使用 PHP 匿名类的各种示例,包括基本用法、实现接口、扩展类、使用构造函数参数和利用 Traits。

作者

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

列出 所有 Python 教程