1

Some add_action() statements that i see now and then are provided with an array in which the first argument is $this, and the second one, the function.

As far as i know, $this stands as the current instance in POO, but what about in this particular case? Let's say for the sake of example that we have a newsletter system that registers your email adress, except if it still exists in the database.

<?php
public function save_email()
{
    if (isset($_POST['zero_newsletter_email']) && !empty($_POST['zero_newsletter_email'])) {
        global $wpdb;
        $email = $_POST['zero_newsletter_email'];

        $row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}zero_newsletter_email WHERE email = '$email'");
        if (is_null($row)) {
            $wpdb->insert("{$wpdb->prefix}zero_newsletter_email", array('email' => $email));
        }
    }
} 

Now we need to declare it as an available action. why should we need an array like this one?

add_action('wp_loaded', array($this, 'save_email'));

And why couldn't we simply do as followed?

add_action('wp_loaded','save_email');
  • 2
    `$this` is the current instance of the current class. So it would be the instance invoking the hook. Or the class the action is being added in. – ArtisticPhoenix Feb 08 '19 at 21:17

2 Answers2

1

Because save_email is a class method and we're adding the action from $this class instance (i.e. from within the class owning the method).

The array represents a PHP callable, see type 3 in the example: http://php.net/manual/en/language.types.callable.php

Danny Battison
  • 493
  • 3
  • 10
0

This is too long for a comment.

$this is the current instance of the current class. So it would be the instance invoking the hook. Or the class the action is being added in.

add_action('wp_loaded', array($this, 'save_email'));

So this is the save email in the current class.

And why couldn't we simply do as followed?

add_action('wp_loaded','save_email');

Because you can't ... Just kidding.

this is due to using call_user_func_array

http://php.net/manual/en/function.call-user-func-array.php

Which is the built in way of doing it. I am sure it also has to do with how functions are called, because without the class it would be ambiguous if this was a function or a method of a class.

In the case you present, PHP would think that was a function. Even if there was a way to tell it, it wasn't, how would you tell it what class that method is in. So it's easier and safer to just put the class in...

Community
  • 1
  • 1
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • you can't because you can't because you can't –  Feb 08 '19 at 21:21
  • I see, thanks a lot for the feedback. I'll just take it as an internal logic cog and assume that it's how it's suppose to be – FitzChevalerie Feb 08 '19 at 21:30
  • you can also use `[__CLASS__, 'method']` if you want a fresh instance. And for a static you can do `__CLASS__.'::method'` Without those we could only do functions, (if you call a Closure a function) – ArtisticPhoenix Feb 08 '19 at 21:33
  • Interresting ! also, i assume that situations where you need a fresh instance for that matter is pretty much edge case, but it's always nice to have in mind that it's possible – FitzChevalerie Feb 08 '19 at 21:45
  • yea `__CLASS__` is just a magic constant for the full class name (namespace and class) of the current class. You could type it in as well. Now you can also do `Someclass::class` which is supper nice. – ArtisticPhoenix Feb 08 '19 at 21:49