class Singleton { private static $PrS='init private'; public static $PuS='init public'; public static function PrS($A=false) { if($A!==false) self::$PrS=$A; else return self::$PrS; } public static function PuS($A=false) { if($A!==false) self::$PuS=$A; else return self::$PuS; } } echo Singleton::PrS(); echo "\n"; echo Singleton::PuS(); // выведет init private // init public echo "\n -- \n"; $D = new Singleton(); echo $D->PrS(); // также выведет init private echo "\n"; // init public echo $D->PuS(); echo "\n -- \n SET them all!"; // А вот здесь Singleton::PrS('changed private'); // меняем переменные класса Singleton::PuS('changed public'); // используя статическую ссылку echo "\n"; // и попробуем проверить их из "созданного" класса (хотя это просто ссылка копия) echo $D->PrS(); // разумеется, выведет: changed private echo "\n"; echo $D->PuS(); // changed public
Copy this simple example, run it, and you will understand that the static class, together with its static variables, is not duplicated by the new operator. Yes, at least ten duplicates do it - all the same - static variables will remain.
The word static for the function indicates that the address in the global table when it is compiled is already allocated - it is strictly allocated. Similarly with static variables - their address is also static. And nonstatic variables (classes) do not exist in the address space until they are defined (by the operator new). There is nowhere to turn. For static, the address is already there - and you can always access it (the variable) - someStaticClass :: value
If you want to use a static class for working with a database, get a private static variable DB_handler inside. It is necessary to work with several connections (several databases) - get it as needed. You can even build something of a static array. Why not? You will need to dodge too much - rewrite the class. Those. copies of static classes do not differ at all (when they are made by the operator new), until at least one non-static variable appears in them. True, after that they will differ only in this non-static variable. True, at the same time, to manage this variable is already obtained only from the manufactured class.
Further:
public static function getInstance() { if ( is_null(self::$instance) ) { self::$instance = new Singleton; } return self::$instance; }
This is just a piece of code that returns a copy of the link to the address of the static class Singleton. This is the same as writing Singleton: :( and there is something)
That is what the question was about - "WHY?". The answer is simple - yes, nothing. :)
Probably, there are tasks where you need to make a copy of the class Singleton, " ... but if there were no appeals (well, something was not needed), then nothing will start and everything will be quiet and calm ... But it seems like a static class will exist even when it may not be needed ... and oh, how terrible will it be to eat memory ... "In general, somehow I cannot come up with such a task right now to apply the CREATION of classes instead of static classes.
And here, for example, I also don’t see the difference between complex Singleton and simple Singleton :: doAction (). In general, static classes (with static variables) are also extremely convenient because they provide "global" variables for any area of visibility. And the handler for the database is a vivid example.