WordPress.org

Make WordPress Core

Opened 11 months ago

Last modified 9 months ago

#21300 new feature request

Implement autoloader compatible with PSR-0 for plugins and themes

Reported by: dave1010 Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version:
Severity: normal Keywords:
Cc:

Description

To provide more compatibility with other frameworks, it would be great if WordPress implemented an autoloader compatible with PSR-0. Plugins and themes could then register namespaces or prefixes with the autoloader.

I believe this could be implemented without any BC breaks.

Details of the PSR-0 standard:
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

Change History (5)

comment:1 nacin11 months ago

By registering, I imagine you mean the code that implements spl_autoload_register(). SPL can be disabled before 5.3, so we can't consider it to be widely available. I suspect we will look into autoloading and PSR-0 when moving to PHP 5.3 is more than just a theoretical discussion — I don't see a minimum requirements change occurring before 2014, at the very earliest.

comment:2 scribu11 months ago

  • Component changed from Filesystem to General
  • Summary changed from Implement autoloader compatible with PSR-0 to Implement autoloader compatible with PSR-0 for plugins and themes

Let's be clear that this would only work with plugin/theme code. Core classes are not named "correctly" according to PSR-0 and to rename all of them is simply not feasible.

Maybelater?

comment:3 follow-up: dave101011 months ago

At first I had hoped that core classes could be made compatible too but a quick grep shows around 200 classes in a clean WP install, with fairly inconsistent naming (https://gist.github.com/3135160). A long-term future issue may be to try to clean these up (WordPress 4.0?). An autoloader just for plugins and themes would still be great and encourage consistency.

Though SPL can be disabled, I believe spl_autoload_register is standard from PHP 5.1.2. I haven't seen any installations without it. Are there any other extensions (apart from mysql) that could be disabled that WordPress requires?

If SPL is likely to be disabled then the code could be wrapped in a function_exists(). Alternatively the autoloader could only be ran if PHP >= 5.3.0 (and include support for namespaces). Plugins and themes that would like to use a PSR-0 autoloader are quite likely to only support PHP 5.3 anyway. This does, however, introduce a big difference between 5.2 and 5.3, which will make testing problematic.

I still believe this is a great benefit we can give to users running PHP 5.3+ that is worth the disadvantage of the code diverging.

For reference, here's discussion on PSR-0 being implemented in Kohana and Drupal.

Version 0, edited 11 months ago by dave1010 (next)

comment:4 in reply to: ↑ 3 SergeyBiryukov11 months ago

Replying to dave1010:

Are there any other extensions (apart from mysql) that could be disabled that WordPress requires?

JSON (#18015, #19524).

comment:5 griffinjt9 months ago

+1 to this. Using a PSR-0 complaint autoloader would be an excellent resource for plugins and themes to use.

I've implemented a PSR-0 compliant autoloader in Soliloquy and so far there have been no issues (over 10k+ downloads total for free and paid) that have been reported.

Note: See TracTickets for help on using tickets.