Learn

Core

Don't repeat repeat yourself

The Core is a place for you to gather all your add-on’s processing methods. It is a way for you to keep your code DRY.

All other aspects in your add-on (plugin, fieldtype, hooks, API, tasks) have access to your add-on’s Core object via $this->core. This is a simple reference to the Core object itself.

The Core object itself doesn’t get a $this->core helper, because it would reference itself, and inside that would be a reference to itself — the dreaded infinite loop.
Anyway, a method within a Core that needs access to another method in that same object will be using $this->, so you’re covered.

A common paradigm

Sometimes it’s very possible that your add-on may perform the same function from multiple points. For example, you might want to do something inside a hook, and from within a plugin, and from an API, and in the background with a task.

// {{ my_plugin:something }}
class Plugin_my_plugin extends Plugin
{
  public function something()
  {
    return $this->core->doSomething();
  }
}
// URL: /TRIGGER/my_plugin/something
class Hooks_my_plugin extends Hooks
{
  public function my_plugin__something()
  {
    exit($this->core->doSomething());
  }
}
class Core_my_plugin extends Core
{
  public function doSomething()
  {
    return 'Something';
  }
}

Tasks?

If you remember hearing that Tasks was the place to put your common code, you’d be right.
We’ve changed Tasks’ focus to just be background processing. However, Tasks still act like Core for legacy purposes.

This article was last updated on September 13th, 2016. Find an error? Please let us know!