Usage of PHP anonymous classes

Time:2021-12-29

After PHP 7, the feature of anonymous classes has been added to PHP. Anonymous classes and anonymous methods make PHP a more modern language and make our code development more and more convenient. Let’s first look at the simple use of anonymous classes.

//Direct definition
$objA = new class

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

//Method
function testA()
{
    return new class

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

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

//As a parameter
function testB($testBobj)
{
    echo $testBobj->getName();
}
testB(new class{
        public function getName()
    {
            echo "I'm testB's obj";
        }
    });

This paper gives three ways to use anonymous classes at one time. Anonymous classes can be defined directly to variables, returned in methods using return, or passed inside methods as parameters. In fact, an anonymous class is like a class that is not defined in advance, and it is instantiated directly at the time of definition.

//Inheritance, interface, access control, etc
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();

Like ordinary classes, anonymous classes can inherit other classes and implement interfaces. Of course, they also include various access control capabilities. In other words, anonymous classes are no different from ordinary classes in use. But if you use get_ The class name obtained by class () will be the class name automatically generated by the system. Of course, the same anonymous class returns the same name.

//The name of the anonymous class is given by the engine
var_dump(get_class($objC));

//For the same anonymous class declared, the created objects are instances of this class
var_dump(get_class(testA()) == get_class(testA()));

What about static members in anonymous classes? Of course, like ordinary classes, static members belong to classes rather than instances.

//Static variable
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. This is also a feature of static members of ordinary classes.

Test code:
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

Reference documents:
https://www.php.net/manual/zh/language.oop5.anonymous.php

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532