[wp-hackers] Passing class methods by reference

Andrew Nacin wp at andrewnacin.com
Thu Aug 5 18:21:00 UTC 2010


On Thu, Aug 5, 2010 at 1:52 PM, Mike Schinkel
<mikeschinkel at newclarity.net>wrote:

> Jacob, I was actually trying to figure out how to remove a filter that was
> added using an array instance method just the other day and was stumped.
>  Mine sharing how it's done?
>

class my_class {
   function __construct() {
     add_action( 'init', array( &$this, 'init' ) );
   }
   function init() {}
}
$my_class_instance = new my_class;
remove_action( 'init', array( $my_class_instance, 'init' ) );

This is generally why I always assign instantiations to a variable in my
plugins (versus simply calling `new myclass;`), that way I'm playing nice
with others and allowing another plugin to modify the callbacks I attach. I
believe I mention this as good practice in <
http://www.andrewnacin.com/2010/05/11/in-wordpress-prefix-everything/>.

We included similar code in Twenty Ten, removing the default CSS for the
Recent Comments widget:
<
http://core.trac.wordpress.org/browser/tags/3.0.1/wp-content/themes/twentyten/functions.php#L421
>


More information about the wp-hackers mailing list