[wp-trac] [WordPress Trac] #33413: most PHP classes should be in their own file

WordPress Trac noreply at wordpress.org
Tue Aug 18 23:01:13 UTC 2015


#33413: most PHP classes should be in their own file
----------------------------+--------------------------
 Reporter:  wonderboymusic  |      Owner:
     Type:  enhancement     |     Status:  new
 Priority:  normal          |  Milestone:  4.4
Component:  General         |    Version:  trunk
 Severity:  normal          |   Keywords:  dev-feedback
  Focuses:                  |
----------------------------+--------------------------
 WordPress has a weird way of bundling things in files: symbols, functions,
 classes, top-level code, the works. This can get increasingly bad over
 time. If we were to start WP anew today, there is no way we would organize
 our code in this fashion. This begs the question: are we open to cleaning
 some of these things up, or do we need these files to continue to
 arbitrarily include the works?

 PSR-1, if anyone cares, suggests:
 {{{
 Files SHOULD either declare symbols (classes, functions, constants, etc.)
 or cause side-effects (e.g. generate output, change .ini settings, etc.)
 but SHOULD NOT do both.
 }}}

 I am not suggesting that we adopt PSR-1, but ...

 In #30947 and #32529, I was able to remove top-level code from several WP
 files. This allows the remaining files to only declare functions.
 `default-filters`, `admin-filters.php`, and `ms-admin-filters.php` contain
 only function calls (top-level code). One remaining irksome piece: there
 are some multi-thousand line files in the codebase that declare a swath of
 functions, and then, seemingly out of nowhere: they declare a class.

 There are several files in `wp-includes` that declare classes properly: 1)
 in their own file and 2) using a consistent naming convention:
 `class-{DASHED LOWERCASED CLASS}.php`. All classes should use this
 convention.

 Forgetting that some of our classes are way too big and do way too much,
 some of these files contain hundreds/thousands of lines of function
 declarations and then still also declare a class of 100s/1000s of lines
 long.

 I think we can see where this is heading, I would like to propose the
 creation of class files for:

 {{{
 WP_Comment_Query
 WP_Http_Cookie
 WP_Http_Curl
 WP_Http_Encoding
 WP_HTTP_Proxy
 WP_Http_Streams
 WP_Http
 WP_Meta_Query
 WP_Post
 WP_Query
 WP_Rewrite
 WP_Role
 WP_Roles
 WP_Tax_Query
 WP_User_Query
 WP_User
 WP_Widget_Factory
 WP_Widget
 }}}

 We had a similar problem with Media JS, which was alleviated by me in
 [31373]. It is WAY easier to absorb complex code when these units are
 broken up.

 I have a patch, and I welcome feedback.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33413>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list