Ready to get started?Download WordPress


Add object-oriented foundations and design patterns

  1. npetetin

    Since the last releases, more and more classes have been introduced as part of core WordPress, which makes it de facto more and more object-oriented. However, more advanced object-oriented paradigms are still missing. Among those paradigms: polymorphism (open/closed principle) and design patterns.

    Introducing an object factory instead of the use of "new" statements would allow extending core and plugin classes by simple polymorphism and then creating tons of new extension opportunities that go far beyond the current action/filter model (which would remain great and useful) by facilitating the process of writing object-oriented plugins.

    In order to stick to the WordPress way, the design patterns to be implemented should be:
    * Generic Factory: the class name and the construction arguments would be passed as arguments
    * Static Proxy: even if the factory itself would be an object, it would be accessed through a normal and classical function

    The Singleton design pattern would also be a natural and useful addition.

    I filed a feature request in the Trac bug tracker with a proposed implementation for this new API. To make it short and sweet, the basic services offered would be:
    * Register a class: set the class name and the class file, as well as the class it’s optionally a substitute of by extension
    * Register an object: set the unique name and polymorphic class of a singleton that may be called later
    * Get a registered object: provide with the named singleton created from the right registered class
    * Create an object: provide with a new object created from the right registered class

    Such an object-oriented foundation might also be the first stone of a fully object-oriented WordPress in the future, who knows...

    Posted: 2 years ago #
  2. lamhud

    I think adapting available object oriented frameworks to WP is the way to go. Take Zend Framework for example. It's open source. It supports inheritance, polymorphic associations and
    polymorphic queries. It supports design patterns. It has APIs.
    A plugin adding Zend to WP is already available

    Posted: 1 year ago #
  3. grezvany13


    I think that the current codebase, and the vision of backwards compatibility, will make it hard to fully rewrite WP to a full OOP framework.

    However, if I look at existing code (like WP_Widget, WP_Query, WP_User, etc.) the approach of using (semi) abstract classes is a huge step forward.

    For me most important reason to go OOP is performance, since it allows features as autoloading (when done correctly!)
    Especially on plugin heavy websites it would be great to only load the code which is actually used, instead of everything and hope it doesn't crash the server...

    Posted: 7 months ago #
  4. juanvillegas


    Yea, at least some direction on how to use oop in wordpress themes in a "standardized" way would be great. I hate having to adapt myself to every single theme i buy, because each author uses a different approach on where to place classes, how to use mvc (if they code that way), how to handle autoloading, etc.

    Posted: 7 months ago #

RSS feed for this topic


You must log in to post.

  • Rating

    27 Votes
  • Status

    This idea is under consideration