[wp-hackers] Passing class methods by reference

Jacob Santos wordpress at santosj.name
Thu Aug 5 23:50:38 UTC 2010


On 8/5/2010 1:51 PM, Mike Schinkel wrote:
> Thanks. So if a plugin did not create an instance then it's pretty 
> much not possible to remove the hooks they add, correct?

The simple answer: Yes. The real answer is that it makes it extremely 
difficult. You would have to manually go into the $wp_filter global and 
remove the instance and really any instance that matches that class name 
to be sure, which might be what you wanted to do anyway.

> Okay, so here comes another question tangentially related question.  I see a lot of people using classes for plugins and initializing them as plugins but try as I might I can't see the benefit of doing that over using static class methods.
>    

I have stated in the past that using array(&$this, 'method') was really 
bad, for performance, reference, and possible bugs it causes. However, 
many people swear by initializing plugins as it allows them to have 
multiple classes with different data assigned to them for them to 
reference. As for this reasoning, very rarely is it done first of all, 
and secondly, if that was the case, then best practices would dictate 
using a separate class and referencing that instead. The real reason is 
that it is easy and programmers are too lazy and, or ignorant of PHP and 
static methods to properly use them in a plugin.


> 1.) it only adds one global symbol, i.e. only the class name and not a global variable too.
>    

This is second only to functions in terms of speed as well.

> 2.) There's no benefit to using instances (that I've identified) when the methods are used for hooks; we only need a "singleton."
>    

Well, not a Singleton as technically you can do:

$first = new my_class();
$second = new my_class();

Not that it would do anything, but the Singleton pattern would prevent 
you from creating two instances of the same class.

> 3.) Using this pattern the plugin user can easily remove filters without me having (to remember to) define a variable in the global namespace.
>    

It would also prevent the majority of bugs that people would cringe over 
tracking down and attempting to fix.

> So, the questions are:
>
> 1.) Am I missing something here why it might be preferable to use an instance rather than a class?
>    

The reason it is preferable is that it is prettier to not use static 
methods. This preference depends on what you think about object purity. 
If you don't care, then either way is fine. It is possible to use the 
static method way in both PHP4 and PHP5, except that you can't define 
that a method is static and thus may be used as part of the object 
instance in PHP4. PHP5 will also throw eggs at you for not using static 
in the function definition.

> 2.) If I'm not missing something why have we not promoted the pattern above rather than the instance equivalent (other than it just not occurring to people?)
>    

I have promoted it since 2.3, but what do I know? Truthfully, it is not, 
nor should it ever be part of WordPress doctrine to tell plugin 
programmers how they should develop their plugins, unless it involves 
security, which this does not. It is an annoyance and nothing more.

Getting people to follow the pattern requires respected plugin 
programmers in the community to start using the pattern. The rest will 
follow as they pick it up.

I think for the longest time, people were following the terrible pattern 
found in the Automattic Widgets, where they were placing two functions 
in a single function to encapsulate the functions. It wasn't until I and 
a bug proved what I stated originally that it was a bad idea. Beginner 
programmers are adorable, are they not? They follow what the witness 
from developers who are popular and if those developers aren't the best 
and use practices not the best, then it is no wonder others follow a 
similar pattern.

If this email sounds harsh, it is because it is meant to be. Programmers 
(myself included) think that their code is great and rationalizes their 
code based on reasons that justify why their code is that way. Given the 
nature of coding, that if it solves a problem, then who cares how 
terrible. It is reasonable to assume that this problem will continue for 
a number of years. Until of course, the SPL function can be used and 
everyone can sleep soundly knowing that they'll be able to remove 
plugins that at least keep a global instance.

Jacob Santos


More information about the wp-hackers mailing list