#2370 closed defect (bug) (fixed)
bbPress private static $instance variable is infinitely nested in main bbPress singleton
Reported by: | johnjamesjacoby | Owned by: | johnjamesjacoby |
---|---|---|---|
Milestone: | 2.4 | Priority: | normal |
Severity: | normal | Version: | 2.1 |
Component: | Tools - Code Improvements | Keywords: | has-patch |
Cc: | nashwan.doaqan@… |
Description
Had a hard time coming up with a more succinct title for this ticket, so I'm attaching a screenshot to illustrate what's happening.
Let's start with the $instance variable in bbpress.php. This private static variable is intended to contain a unique singleton instance of bbPress, primarily to avoid polluting the global scope with global variables.
The problem is that, since the $instance is a pointer to the bbPress singleton, each pointer to $instance also contains a pointer to $instance.
What problem does this cause? Nothing performance wise (thankfully) but only because PHP is smart enough to not create new infinite instances, and instead creates infinite references to the same instance. Strangely, PHP doesn't seem to complain about this recursive pointing, which is how it's gone unnoticed.
Why fix it? Because it's confusing to anyone poking around the bbPress singleton, and it's just kind of a dumb mistake.
An immediate and easy fix is to remove the private static variable in lieu of a local static variable inside the instance() method. This effectively avoids the recursion, and retains the same singleton approach.
Attachments (2)
Change History (5)
#2
@
11 years ago
Interesting! I have used the old technique in some of my projects!
So is the generic way of how we implement the singleton pattern is wrong?!
http://www.phptherightway.com/pages/Design-Patterns.html
And another question: What is the app you had used to generate the screenshot?
$bbp recursion tree