Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
来自 flightphp 框架文档:
默认情况下,每次加载类时,您都会获得一个共享实例。要获取一个类的新实例,只需传入 false 作为参数:
// Shared instance of the class $shared = Flight::db(); // New instance of the class $new = Flight::db(false);
什么是共享实例?这两种类型的行动有什么区别?
Flight::db() 是一个返回类实例的静态方法。
通常使用单例模式,这意味着,如果多次调用 Flight::db(),所有变量都指向同一个实例。
如果你调用 Flight::db(false),每次调用都会创建一个新对象,这意味着如果你多次调用它,每次调用都会得到一个自己的对象。