So....
There are obviously many questions that everyone has been asking about singletones, global state variables, and all these wonderful things. My question is:
If singletons and globals are so bad, why are they used so often?
The following examples are just those that I think are used by good pieces of people.
I give you a function from CodeIgniter that uses the psuedo-singleton function:
(system \ codeigniter \ Common.php line 89)
function &load_class($class, $instantiate = TRUE) { static $objects = array();
Having placed each object in one registry, you cannot use your load_class function to create multiple instances of something. This is especially inconvenient if you want to use classes as data structures.
In addition, since there is only one instance of all these classes, it leads to an argument against the Global State. Which leads me to .....
The whole Wordpress system, which works mainly with global variables. All data for looping messages are placed in various global variables.
(wp-includes \ query.php line 2644)
function setup_postdata($post) { global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages; $id = (int) $post->ID; $authordata = get_userdata($post->post_author); .... }
These are just two basic examples of frameworks that use Singleton / Globals as the basis for their entire system!
So ... is it simply because these systems did not catch up with the OOP methodology? It just doesnโt make sense when you have so many people who tell you not to use global variables or singlets, so that your whole system is based on the mentioned practices.
Of course, there is an argument about backward compatibility with PHP4. I still think that there were ways to program OOP in PHP4, since classes were still available.