-1

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?

Mohammad Salehi
  • 565
  • 1
  • 12
  • 33

1 Answers1

0

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.

A Loewer
  • 176
  • 5
  • what if I call Flight::db() in multiple class and function? are they all point to the same instance? – Mohammad Salehi Mar 19 '18 at 11:45
  • that is exactly how a singleton is supposed to work. Whenever you call it, no matter from where, you always get the same instance, as long as you are in the same server call. – A Loewer Mar 19 '18 at 14:02