Home > Backend Development > PHP Tutorial > 怎样通过 PHP 实现延迟初始?

怎样通过 PHP 实现延迟初始?

WBOY
Release: 2016-06-06 20:42:54
Original
1084 people have browsed it

请举例详解。

回复内容:

请举例详解。

简单的如下:

<code>class LazyInit()
{
    public static $instance;
    public static function  doSomething(){
        if($instance == null) {
            self::$instance = new YourService();
        }
        return self::$instance->doSomething();
    }
}
</code>
Copy after login

调用方法时才执行实例化操作,其他实现的一些参考可以看:闭包实现
这种方式不适合一些每次访问必须初始化的类。

Related labels:
php
source:php.cn
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