This article describes the usage of PHP7 anonymous class. Share it for your reference, as follows:
<?php /** * Created by PhpStorm. * User: Itboot * Date: 2019/1/17 * Time: 18:15 */ class An { private $num; protected $age = 15; public function __construct() { $this->num = 1; } protected function bar(): int { return 10; } public function drive() { return new class($this->num) extends An{ protected $id; public function __construct($num) { $this->id = $num; } public function ea() { return $this->id + $this->age + $this->bar(); } }; } } echo (new An())->drive()->ea();
<?php $fun = function (){ print 'This is an anonymous function'. PHP_EOL; }; $fun(); class Animal { public $num; public function __construct(...$args) { $this->num = $args[0]; } public function getValue($su): int { return $this->num + $su; } } $an = new Animal(4); echo $an->getValue(12) . PHP_EOL; echo 'Anonymous Class'. PHP_EOL; echo (new class(11) extends Animal{})->getValue(12);
For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.