WordPress.org

Ideas

Add object-oriented foundations and design patterns

  1. npetetin
    Member

    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: 4 years ago #
  2. lamhud
    Member

    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: 4 years ago #
  3. grezvany13
    Inactive

    12345

    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: 2 years ago #
  4. juanvillegas
    Inactive

    12345

    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: 2 years ago #
  5. PCW-Meyer
    Member

    Backwards compatibility is a big issue here though. Making these changes may break a lot of old installations.
    A lot of blogs could then maybe not update their installations leaving them vulnerable to potential existing security flaws.

    I totally agree that WP would be much easier to handle for developers if proper object oriented methods would be used. If there is a way to maintain backwards compatibility I think this is the way forward

    Posted: 1 year ago #
  6. cricketoutlook
    Inactive

    @PCW security flaws can also be covered with this type of update and all previous installations should be thrown to users as update many of them will update it. It's really nice idea also.

    Posted: 1 year ago #
  7. marsjaninzmarsa
    Member

    12345

    +1 for more OOP, -1 for Singletron.

    Posted: 1 year ago #
  8. juanvillegas
    Inactive

    12345

    singleton IS oop, what are you talking about!?

    Posted: 1 year ago #
  9. marsjaninzmarsa
    Member

    12345

    I just don't like Singleton, it's anti-pattern for me.

    Posted: 1 year ago #
  10. I use the zend framework and it works great for me. As lamhud already said, it has many benefits. Simple use the plugin and add it to WP.

    Posted: 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    12345
    92 Votes
  • Status

    This idea is under consideration