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

Detailed explanation of new features of PHP: anonymous class

小云云
小云云Original
2018-03-08 14:10:521360browse

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());?>

Result

object(class@anonymous)#2 (0) {}

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!

Statement:
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