匿名类

匿名类很有用,可以创建一次性的简单对象。

<?php

$foo = new class {
    /**
     * Just a test property.
     *
     * @var string
     */
    public string $property = 'foo';
};
var_dump($foo);           // object(class@anonymous)#1 (1) { ["property"]=> string(3) "foo" }
var_dump($foo->property); // string(3) "foo"

可以传递参数到匿名类的构造器,也可以扩展其他类,实现接口,以及像其他普通的类一样使用 Trait 。

<?php

class Foo
{
    /**
     * Just a test property.
     *
     * @var string
     */
    public string $property = 'foo';
}

interface Bar
{
    /**
     * Just an abstract method.
     *
     * @param  string $string
     * @return string
     */
    public function method(string $string): string;
}

trait Baz
{
    /**
     * Return a string.
     *
     * @param  void
     * @return string
     */
    public function test(): string
    {
        return 'baz';
    }
}

$qux = new class extends Foo implements Bar {
    use Baz;

    /**
     * Override the abstract method.
     *
     * @param  string $string [description]
     * @return string
     */
    public function method(string $string): string
    {
        return $string;
    }
};
var_dump($qux);                // object(class@anonymous)#1 (1) { ["property"]=> string(3) "foo" }
var_dump($qux->method('qux')); // string(3) "qux"
var_dump($qux->test());        // string(3) "baz"

匿名类被嵌套进普通类后,不能在匿名类中访问这个外部类的 privateprotected 方法或者属性。为了访问外部类 protected 属性或方法,匿名类可以 extends 此外部类。 为了使用外部类的 private 属性,必须通过构造函数传进来。

<?php

class Foo
{
    /**
     * Just a test property.
     *
     * @var int
     */
    protected int $foo = 1;

    /**
     * Just a test property.
     *
     * @var int
     */
    private int $bar = 2;

    /**
     * Return an integer.
     *
     * @param  void
     * @return int
     */
    protected function method(): int
    {
        return 3;
    }

    /**
     * Return an anonymous class.
     *
     * @param  void
     * @return Foo
     */
    public function call(): Foo
    {
        return new class($this->bar) extends Foo {
            /**
             * Just a test property.
             *
             * @var int
             */
            private int $baz;

            /**
             * Initialize the value of the property.
             *
             * @param  int
             * @return void
             */
            public function __construct(int $value)
            {
                $this->baz = $value;
            }

            /**
             * Calculate the sum of all expressions.
             *
             * @param  void
             * @return int
             */
            public function test(): int
            {
                return $this->foo + $this->baz + $this->method();
            }
        };
    }
}

$foo = new Foo();
var_dump($foo->call()->test()); // int(6)

声明的同一个匿名类,所创建的对象都是这个类的实例。

<?php

/**
 * Return an instance of an anonymous class.
 *
 * @param  void
 * @return class@anonymous
 */
function foo()
{
    return new class {
        //
    };
}

if (get_class(foo()) === get_class(foo())) {
    var_dump(true);
} else {
    var_dump(false);
}
// bool(true)

results matching ""

    No results matching ""