How bad are singletones?

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)

/** * Class registry * * This function acts as a singleton. If the requested class does not * exist it is instantiated and set to a static variable. If it has * previously been instantiated the variable is returned. * * ...... */ function &load_class($class, $instantiate = TRUE) { static $objects = array(); // Does the class exist? If so, we're done... if (isset($objects[$class])) { return $objects[$class]; } ....... } 

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)

 /** * Setup global post data. * *.... */ 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.

+6
oop php codeigniter wordpress
source share
7 answers

Since it is relatively simple to work with singletones and works without more detailed planning of the structure of your application. I asked a question about alternatives some time ago and got interesting answers.

+7
source share

People discourage the use of global variables because it increases the likelihood of errors. It is much easier to make a mistake somewhere if every function in your program accesses the same global variables and is much more difficult to debug. It is also much more difficult to verify.

I guess they are still used so often because programmers are lazy . We donโ€™t want to spend time making the code organized and beautiful, we just want to do the work . It is much easier to just write a global function / variable / regardless of its modulation, and after you start this way, there is too much pain to come back and refactor . Perhaps the reason: they started and just never returned.

+3
source share

Perhaps because of GoF's Design Patterns book. This has become too common, and people find it unmistakable.

+3
source share

The reasons for PHP4-based applications, such as WP or CI, are partly due to the fact that PHP4 supports OOP constructs worse.

Globals and singletones are also simple: it takes less to think about patting something globally than creating it with good OOP practice. It is also easier to access them, just specify the code in the name and that it is instead of having to transfer the object from another place.

One negative side effect of the global state (global variables, singletones, etc.) is that it makes things a lot more complicated than unit test.

ps: In my opinion, Wordpress as a whole has pretty low quality code. I would not use it as an indicator of everything ...

+3
source share

If singletons and globals are so bad, why are they used so often?

I think the bottom line is that the Singleton makes life easier. At least at first glance. I donโ€™t have enough experience to say anything more useful, however I found the following good post:

Singleton is considered stupid (Steve Yegge)

+2
source share
  • Global variables and singletones are popular because they are simple and convenient.
  • In the PHP community, dependency injection is still pretty unknown, the only reasonably convenient replacement for global things.
  • The PHP community often prefers simple hacks over the right solutions.
  • Many PHP developers are completely unaware of programming and know little enough to make them work.
+2
source share

During cleaning, a singleton will hide dirt in a dark corner instead of cleaning.

It is used so widely because it handles problems very well.

This is good for those who need to clean but do not want to.

This is bad for those who actually clean something.

Consider dependency injection http://martinfowler.com/articles/injection.html if Singleton causes a problem that you can no longer hide.

+1
source share

All Articles