[wp-hackers] [help] sink plugin hooks for $this

Owen Winkler ringmaster at midnightcircus.com
Sat Jan 14 16:21:27 GMT 2006

Jamie Talbot wrote:
> I know about $this and I'm pretty sure we're saying the same thing.  Both of the following methods
> are possible.  The first is much cleaner, but the second is still valid and works.  I wasn't saying
> you should do it, just that it could be done!

<snip first method>

> class myTestClassB() {
>     function myTestClassB() { // constructor
>     }
>     function install() {
> 	global $singletonB;
>         // do install stuff here
>     }
> }
> $singletonB = new myTestClassB;
> add_action('activate_samba/loadbalancer.php', array('myTestClassB',  'install');
> ?>

This option does not allow $this to be used.  Calling a class method 
statically to call methods on a global instance of an object seems like 
a bad idea.

If you're going to use a static method as the sink for add_action(), 
then you should skip the global instance and call *all* of your 
functions statically:

class myTestClassB() {
	function install() {
	function doSomething() {
add_action('hook_name', array('myTestClassB', 'install'));

Of course, if you do it that way, you lose all of the benefit of an 
object instance, such as the ability to use $this and class member 
variables.  You may wish you had created an object.

In my experience, it has been better to create an object instance in 
which the constructor adds sinks.

While I'm thinking about it, would it be useful to produce a plugin 
framework class, if not for the core then for general use?

Something like (LIKE this, NOT this):

class pluginFramework {
	function pluginFramework() {
	// Do generic init stuff here?
	function register_sinks() {
		$sinks = array_filter(get_class_methods($this), array(&$this, 
		foreach($sinks as $sink) {
			add_filter($sink, array(&$this, '__'.$sink))
	function filterSinks($name) {
		return(substr($name, 0, 2) == '__');
	function debug() {
	// Generic plugin debug stuff here...

And then you could do simple stuff like:

class myPlugin extends pluginFramework {
	funciton myPlugin() {
	function __admin_head() {
	// Output stuff in the admin head
	function __admin_footer() {
$myplugin = new myPlugin();

Such a framework could include functions to do more complicated things 
like mess with TinyMCE, handle Ajax requests 
($this->handle_ajax('function_name'); //!!), or respond differently when 
the plugin file is requested directly, and would save me from having to 
package those generic functions into every plugin I produce.


More information about the wp-hackers mailing list