Skip to:
Content

bbPress.org

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#2370 closed defect (bug) (fixed)

bbPress private static $instance variable is infinitely nested in main bbPress singleton

Reported by: johnjamesjacoby's profile johnjamesjacoby Owned by: johnjamesjacoby's profile 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)

Screen Shot 2013-07-23 at 11.30.12 AM.png (108.2 KB) - added by johnjamesjacoby 11 years ago.
$bbp recursion tree
2370.patch (1.4 KB) - added by johnjamesjacoby 11 years ago.
Remove private static $instance and use a local static variable in instance() method

Download all attachments as: .zip

Change History (5)

@johnjamesjacoby
11 years ago

Remove private static $instance and use a local static variable in instance() method

#1 @johnjamesjacoby
11 years ago

  • Resolution set to fixed
  • Status changed from new to closed

In 5047:

Switch main bbPress singleton pointer from a private static to a local static inside the instance() method, preventing recursive $instance references. Fixes #2370.

#2 @alex-ye
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?

#3 @alex-ye
11 years ago

  • Cc nashwan.doaqan@… added
Note: See TracTickets for help on using tickets.