Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#22958 closed feature request (maybelater)

Add object-oriented foundations and design patterns

Reported by: npetetin Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:


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.

Here attached is 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...

Attachments (1)

wp-object-oriented-foundations.diff​ (21.0 KB) - added by npetetin 3 years ago.
Patch for WordPress object-oriented foundations

Download all attachments as: .zip

Change History (4)

3 years ago

Patch for WordPress object-oriented foundations

#1 @scribu
3 years ago

Hello npetetin,

The idea is nice and all, but what's the benefit? We tend not to add new APIs unless they have immediate use in Core.

Therefore, I don't think you'll be able to get much traction until you make at least 2 class/object systems in WordPress use this foundation, which would at least prove its flexibility.

Last edited 3 years ago by scribu (previous) (diff)

#2 @nacin
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

As discussed in #21309, there are already some opportunities to extend these new objects via composition. This is a fine idea, but we're just not close to discussing factories at this time. It will be a long while before a number of things that should be (non-stdClass) objects, are (non-stdClass) objects. There should be no plans to make this extensible via plugin-based polymorphism until we have fully grasped and defined the direction in which we are headed. It's not like we're removing control from plugins as things get converted to proper objects; as you point out, there is an existing abundance of hooks that already provides a good framework.

New extension opportunities are nice and all (to borrow from scribu), but we need to walk, not run.

#3 @npetetin
3 years ago

From my experience in Software Architecture and Engineering, the benefits of introducing an early polymorphic object factory while adopting an object-oriented approach are numerous and immediate. But I understand and I respect your positioning here.

My plan is still to share my approach and my experience with the community. Then it will be in the plugin sphere by publishing guidelines and components about it in a near future.


Note: See TracTickets for help on using tickets.