Home  >  Article  >  Backend Development  >  Usage example of PHP7 anonymous class (code)

Usage example of PHP7 anonymous class (code)

不言
不言forward
2019-02-12 14:07:281781browse

The content of this article is about the usage examples (code) of PHP7 anonymous classes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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

The above is the detailed content of Usage example of PHP7 anonymous class (code). For more information, please follow other related articles on the PHP Chinese website!

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