Method Chaining vs. Direct Method Calls in WordPress Plugins

When making a WordPress plugin, you can write your code in different ways. Two common ways are:

  1. Method Chaining (Object Chain): Calling multiple methods one after another in a single line.
  2. Direct Method Calls: Calling each method separately.

We will explain both methods with examples, talk about the good and bad things about each, and see if one is faster than the other.


Method Chaining (Object Chain)

What Is It?

Method chaining is when you call several functions (methods) on an object in one line. Each method returns $this, which is the object itself, so you can keep adding more method calls.

Example Code:

class MyPlugin {
    public function __construct() {
        // Setup code here
    }

    public function register_post_types() {
        // Code to add custom post types
        return $this;
    }

    public function register_taxonomies() {
        // Code to add custom taxonomies
        return $this;
    }

    public function add_shortcodes() {
        // Code to add shortcodes
        return $this;
    }

    public function add_hooks() {
        // Code to add hooks
        return $this;
    }
}

// Using the class
$plugin = new MyPlugin();
$plugin->register_post_types()
       ->register_taxonomies()
       ->add_shortcodes()
       ->add_hooks();
        

Good Things (Pros):

  1. Shorter Code: You don't have to write $plugin every time.
  2. Flows Nicely: The code looks neat and easy to read.
  3. Easy Sequence: Good for steps that need to happen in order.

Bad Things (Cons):

  1. Harder to Find Errors: If something goes wrong, it's trickier to see where.
  2. Testing is Tougher: It's a bit harder to test each part on its own.
  3. Tiny Bit Slower: Returning $this can make it slightly slower, but usually not noticeable.


Direct Method Calls

What Is It?

This means calling each method one by one, without chaining them together. Each method does its job and doesn't need to return $this.

Example Code:

class MyPlugin {
    public function __construct() {
        // Setup code here
    }

    public function register_post_types() {
        // Code to add custom post types
    }

    public function register_taxonomies() {
        // Code to add custom taxonomies
    }

    public function add_shortcodes() {
        // Code to add shortcodes
    }

    public function add_hooks() {
        // Code to add hooks
    }
}

// Using the class
$plugin = new MyPlugin();
$plugin->register_post_types();
$plugin->register_taxonomies();
$plugin->add_shortcodes();
$plugin->add_hooks();
        

Good Things (Pros):

  1. Clear and Simple: Each step is separate, so it's easy to understand.
  2. Easier to Find Errors: If something breaks, you can see which method caused it.
  3. Flexible Order: You can change the order of the methods or skip some if you want.

Bad Things (Cons):

  1. More Typing: You have to write $plugin-> before each method.
  2. Less Flowing: The code might look a bit clunkier.
  3. Might Miss a Step: Since each call is separate, you might forget to call a method.


Speed Test (Benchmark)

Let's see if one method is faster than the other.

Test Code:

$iterations = 100000;

// Method Chaining Test
$startTime = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
    $plugin = new MyPlugin();
    $plugin->register_post_types()
           ->register_taxonomies()
           ->add_shortcodes()
           ->add_hooks();
}
$chainTime = microtime(true) - $startTime;

// Direct Method Calls Test
$startTime = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
    $plugin = new MyPlugin();
    $plugin->register_post_types();
    $plugin->register_taxonomies();
    $plugin->add_shortcodes();
    $plugin->add_hooks();
}
$directTime = microtime(true) - $startTime;

echo "Method Chaining Time: $chainTime seconds\n";
echo "Direct Method Calls Time: $directTime seconds\n";
        

What We Found:

  • The time difference is very small.
  • Both methods are fast enough for most plugins.
  • For big projects, using direct calls might be easier to manage.

要查看或添加评论,请登录

Dev Kabir的更多文章