魔术方法 __construct(), __destruct() 计算程序运行时间

self与$this的分别
self是引用静态类的类名,而$this是引用非静态类的实例名。

__construct() // 对象产生时,自动执行
__destruct() // 对象销毁时自动执行。

class AdminController extends Controller {
    
    
    public static $count_start_time;
    private $count_start_time_1;
    /**
     * 对象产生时,自动执行
     */
    function __construct()
    {
    
    
        self::$count_start_time =  microtime(true);
        $this->count_start_time_1 =  microtime(true);
        parent::__construct();
    }
    /**
     * 析构函数 __destruct()  对象销毁时自动执行。
     */
    function __destruct()
    {
    
    
        $t1 = self::$count_start_time;
        $t2 = microtime(true);
        echo '<!--耗时'.round($t2-$t1,3).'秒-->';
        // echo '<!--Now memory_get_usage: ' . memory_get_usage() . '-->';

        echo '<!--耗时'.round($t2-$this->count_start_time_1,10).'秒-->';
        // echo '<!--Now memory_get_usage: ' . memory_get_usage() . '-->';
    }

猜你喜欢

转载自blog.csdn.net/appAndWxy/article/details/82381826