#36926 closed enhancement (fixed)
Autoloading: Introduce compatibility shim for spl_autoload_register
Reported by: | rmccue | Owned by: | rmccue |
---|---|---|---|
Milestone: | 4.6 | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch commit |
Focuses: | Cc: |
Description
Split from #36335.
To ensure support for multiple autoloaders when SPL isn't available (5.2 with SPL disabled; 5.3+ require SPL), we should polyfill the autoloader registration code.
Attachments (4)
Change History (18)
#1
@
9 years ago
- Keywords has-patch 2nd-opinion added
- Milestone changed from Awaiting Review to 4.6
Added an actual patch for core; renamed from $spl_autoloaders
to $_wp_spl_autoloaders
to indicate that a) it's part of a shim and shouldn't be used, and b) to avoid any potential variable conflicts. While we could introduce a class with protected/private vars to actually protect it, that's too much overhead for just a shim, especially one that's only loaded on a small number of installs.
This doesn't really introduce any new stuff into core, apart from the compatibility shim for 5.2, so it should be pretty uncontroversial. Core does actually use this with the introduction of Requests, plus we can remove the check in the SimplePie loader (see 36926.2.diff). Based on this, I'd like to get it into core this cycle, regardless of progress on #36335 (although I hope we can get that in too!).
This ticket was mentioned in Slack in #core by rmccue. View the logs.
9 years ago
#3
follow-ups:
↓ 4
↓ 5
@
9 years ago
Hey Ryan,
since you copied stuff from the Autoloader ticket, I do that as well. ;)
We actually spoke about it, but it hasn't made it into your code yet.
Improve __autoload()
$spl_autoloaders
is global, so anyone can put into it whatever they like. So we should check for is_callable
for each autoloader function inside __autoload()
, and continue
if it isn't.
Improve spl_autoload_functions()
Since $spl_autoloaders
is, again, a global, and since we don't want to argue with SPL, we should make sure we're returning an array.
<?php function spl_autoload_functions() { global $spl_autoloaders; return is_array( $spl_autoloaders ) ? $spl_autoloaders : array(); }
#4
in reply to:
↑ 3
@
9 years ago
@rmccue 36926.2.diff seems pretty sane to me.
The obvious breakage is when something else is defining __autoload()
, but I think that's something we're just going to have to expect - there's no way to polyfill it in those cases.
Replying to tfrommen:
Improve
__autoload()
$spl_autoloaders
is global, so anyone can put into it whatever they like. So we should check foris_callable
for each autoloader function inside__autoload()
, andcontinue
if it isn't.
Improve
spl_autoload_functions()
Since$spl_autoloaders
is, again, a global, and since we don't want to argue with SPL, we should make sure we're returning an array.
Both of these seem like needless checks in this case, if outside code modifies the global their warranty is void, just as if they modify the global $wp_filters
arrays, or almost any other global in WordPress for that matter.
#5
in reply to:
↑ 3
;
follow-up:
↓ 6
@
9 years ago
Replying to tfrommen:
We actually spoke about it, but it hasn't made it into your code yet.
Oops, sorry about that! Forgot to incorporate it here.
While I agree with @dd32 that your warranty is void if you do that, I think it makes sense to be defensive here. As it turns out, PHP throws an exception if the callback can't be found, so we should match that. The array check is also cheap, so makes sense to do that.
#6
in reply to:
↑ 5
;
follow-up:
↓ 7
@
9 years ago
Replying to rmccue:
As it turns out, PHP throws an exception if the callback can't be found, so we should match that.
You already support that :) PHP doesn't throw an exception when it's calling the autoloaders (https://3v4l.org/Fj7I1), only when you first try to register it.
#7
in reply to:
↑ 6
@
9 years ago
Replying to dd32:
You already support that :) PHP doesn't throw an exception when it's calling the autoloaders (https://3v4l.org/Fj7I1), only when you first try to register it.
Oh you're totally right, I misread the exception! However, the current patch will throw a warning from call_user_func
, which doesn't match the PHP implementation.
This ticket was mentioned in Slack in #core by ocean90. View the logs.
9 years ago
#9
@
9 years ago
- Keywords 2nd-opinion removed
Added an is_callable check to __autoload
to ensure we don't generate excess warnings. I didn't add the is_array
check, because a) if you directly change that, you've voided your warranty, and b) someone might want to replace it with an array-like object or something, and I don't think there's a need to be super strict.
While this isn't absolutely optimal (I'd be concerned about performance with is_callable
usually), this is only a shim for 5.2, so I don't think performance matters. If you need more performant code, use 5.6+ (ideally, PHP 7).
I think this is ready for commit.
This ticket was mentioned in Slack in #core by rmccue. View the logs.
9 years ago
#12
@
9 years ago
- Owner set to rmccue
- Resolution set to fixed
- Status changed from new to closed
In 37636:
SPL autoload compatibility shim/polyfill