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

Usage example of PHP7 anonymous class (code)

不言
Release: 2023-04-05 07:30:01
forward
1811 people have browsed it

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();
Copy after login
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!

Related labels:
source:csdn.net
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