Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of PHP anonymous classes

Detailed explanation of the usage of PHP anonymous classes

藏色散人
藏色散人forward
2021-02-13 09:14:283884browse

After PHP7, the anonymous class feature was added to PHP. Anonymous classes and anonymous methods make PHP a more modern language and make our code development work more and more convenient. Let's first look at the simple use of anonymous classes.

Recommended: "PHP Video Tutorial"

// 直接定义
$objA = new class

{
    public function getName()
    {
        echo "I'm objA";
    }
};
$objA->getName();

// 方法中返回
function testA()
{
    return new class

    {
        public function getName()
        {
            echo "I'm testA's obj";
        }
    };
}

$objB = testA();
$objB->getName();

// 作为参数
function testB($testBobj)
{
    echo $testBobj->getName();
}
testB(new class{
        public function getName()
    {
            echo "I'm testB's obj";
        }
    });

gives three methods of using anonymous classes at once. Anonymous classes can be directly defined as variables, can be returned using return in methods, or can be passed as parameters to methods. In fact, an anonymous class is like a class that is not defined in advance, but is instantiated directly when it is defined.

// 继承、接口、访问控制等
class A
{
    public $propA = 'A';
    public function getProp()
    {
        echo $this->propA;
    }
}
trait B
{
    public function getName()
    {
        echo 'trait B';
    }
}
interface C
{
    public function show();
}
$p4 = 'b4';
$objC = new class($p4) extends A implements C
{
    use B;
    private $prop1 = 'b1';
    protected $prop2 = 'b2';
    public $prop3 = 'b3';

    public function __construct($prop4)
    {
        echo $prop4;
    }

    public function getProp()
    {
        parent::getProp();
        echo $this->prop1, '===', $this->prop2, '===', $this->prop3, '===', $this->propA;
        $this->getName();
        $this->show();
    }
    public function show()
    {
        echo 'show';
    }
};

$objC->getProp();

Anonymous classes, like ordinary classes, can inherit other classes, implement interfaces, and of course include various access control capabilities. In other words, anonymous classes are no different from ordinary classes in use. But if you use get_class() to get the class name, it will be the class name automatically generated by the system. The name returned by the same anonymous class is of course the same.

// 匿名类的名称是通过引擎赋予的
var_dump(get_class($objC));

// 声明的同一个匿名类,所创建的对象都是这个类的实例
var_dump(get_class(testA()) == get_class(testA()));

So what about static members in anonymous classes? Of course, like ordinary classes, static members belong to the class rather than the instance.

// 静态变量
function testD()
{
    return new class{
        public static $name;
    };
}
$objD1 = testD();
$objD1::$name = 'objD1';

$objD2 = testD();
$objD2::$name = 'objD2';

echo $objD1::$name;

When the static variable in the class is modified, the static variable of all class instances will change accordingly. This is also the characteristic of static members of ordinary classes.

测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/PHP%E5%8C%BF%E5%90%8D%E7%B1%BB%E7%9A%84%E7%94%A8%E6%B3%95.php

The above is the detailed content of Detailed explanation of the usage of PHP anonymous classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete