[wp-hackers] Coding Standards: Functions vs. Classes

scribu scribu at gmail.com
Sun Mar 18 18:39:17 UTC 2012


On Sun, Mar 18, 2012 at 8:27 PM, Brad Touesnard <brad at touesnard.com> wrote:

> But shouldn't register_post_type() and it's related functions be contained
> in a class as they pass around the global $wp_post_types? Those functions
> were written relatively recently.
>

They were introduced in WP 3.0. PHP4 was dropped in 3.2. Besides that, it's
a single list of objects, so it's pretty self-contained.



> Should be interesting to see if globals are abandoned for static class
> variables going forward.
>

I've been thinking about that lately. One advantage of using globals is
that you don't have to repeat the scope each time you access it. Compare:

function register_post_type( $args ) {
global $wp_post_types;

...

if ( !isset( $wp_post_types[ $post_type ] ) )
  $wp_post_types[ $post_type ] = ...

...
}

versus static class variables:

class Post_Type {

public function register( $args ) {
 ...

if ( !isset( self::$wp_post_types[ $post_type ] ) )
  self::$wp_post_types[ $post_type ] = ...

...
 }
}

function register_post_type( $args ) {
return Post_Type::register( $args );
}

Notice the additional boilerplate that this requires. The only advantage of
a static class variable would be that you can make it private.



> I think most of this stuff would make a good wiki article so everyone is on
> the same page. Should we start one?
>

There is no consensus yet, so adding stuff to the Codex is premature.

-- 
http://scribu.net


More information about the wp-hackers mailing list