Make WordPress Core

Opened 3 years ago

Closed 5 weeks ago

#43674 closed enhancement (duplicate)

require() vs require_once()

Reported by: luftaffen Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Bootstrap/Load Keywords: has-patch 2nd-opinion
Focuses: Cc:


When I tried to install wordpress (in openshift, but this should not matter), I got the following message

Fatal error: Cannot redeclare maybe_hash_hex_color() (previously declared in /opt/app-root/src/wp-includes/formatting.php:5759) in /opt/app-root/src/wp-includes/formatting.php on line 5759

I did noticed that formatting.php is being included only twice in the code (using fgrep here):

./wp-settings.php:require( ABSPATH . WPINC . '/formatting.php' );
./wp-includes/load.php:         require( ABSPATH . WPINC . '/formatting.php' );

I solved that by replacing require() with require_once(). And that leads to:
is there a reason not to use require_once() to begin with?

I can submit a patch, but in the end of the day all I did was

for i in $(find . -type f ); do sed -i -e 's/require( /require_once( /' $i; done

Attachments (1)

4367.diff (67.8 KB) - added by luftaffen 3 years ago.

Download all attachments as: .zip

Change History (3)

3 years ago

#1 @johnbillion
3 years ago

  • Component changed from General to Bootstrap/Load
  • Keywords has-patch 2nd-opinion added
  • Version trunk deleted

Thanks for the ticket, @luftaffen . You're probably right that most files should switch to (include|require)_once, but that will need some more investigation, including its performance impact, if any.

The root cause of the problem you're seeing is already being addressed in #43609.

#2 @SergeyBiryukov
5 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

It looks like the issue was resolved in [42988] / #43609.

Using require() should be fine in most cases. Switching to require_once() was previously discussed in #20298, let's continue the discussion there.

Note: See TracTickets for help on using tickets.