phpflightphp

what shared instance and new instance


from flightphp framework documention:

By default, every time you load your class you will get a shared instance. To get a new instance of a class, simply pass in false as a parameter:

// Shared instance of the class
$shared = Flight::db(); 

// New instance of the class
$new = Flight::db(false);

what is shared instance? what is difference between these two type in action?


Solution

  • Flight::db() is a static method that returns an instance of the class.

    Normally a singleton pattern is used, which means, if call Flight::db() several times, all variables point to the same instance.

    if you call Flight::db(false), a new object is created for every call, which means if you call it several times, you get an own object for every call.