Home > Backend Development > PHP Tutorial > Detailed explanation of new features of PHP: anonymous class

Detailed explanation of new features of PHP: anonymous class

小云云
Release: 2023-03-20 22:14:02
Original
1439 people have browsed it

This article mainly shares with you a detailed explanation of the new features of PHP, the anonymous class, and I hope it can help you.

Code

<?phpinterface Logger {
    public function log(string $msg);}class Application {
    private $logger;    public function getLogger(): Logger {
         return $this->logger;
    }    public function setLogger(Logger $logger) {
         $this->logger = $logger;
    }
}$app = new Application;$app->setLogger(new class implements Logger {
    public function log(string $msg) {
        echo $msg;
    }
});

var_dump($app->getLogger());?>
Copy after login

Result

object(class@anonymous)#2 (0) {}
Copy after login

Related recommendations:

Introduction to new features such as anonymous classes, import classes and closure usage in php7

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template