One ton is a static function that allows you to track instances of an object when you use the singleton that you instantiate the object, but instances always remain with the associated object.
Take this example:
$db1 = new Database(); $db2 = new Database();
since you can see that db1 and db2 are 2 new database instances, so itโs not the same there, now take this example.
$db1 = Database::Instance(); $db2 = Database::Instance();
And the code for the instance
class Database { private static $_instance; public static Instance() { if(self::$_instance !== null) {
If you analyze the code, you will be so that no matter where you use the instance throughout the application, your object will always be the same.
a static function is a method inside a class / object is a type of method that can be used without initializing the object.
As for the __callStatic method, this is the Magic Method, which runs where the static method is not available.
For example:
class Database { public static function first() { echo 'I actually exists and I am first'; } public function __callStatic($name,$args) { echo 'I am '. $name .' and I was called with ' . count($args) . ' args'; } }
lets check them out.
Database::first(); //Output: I actually exists and I am first Database::second(); //Output: I am second and I was called with 0 args Database::StackOverflow(true,false); //Output: I am StackOverflow and I was called with 2 args
Hope this helps you
RobertPitt
source share