[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 <

We included similar code in Twenty Ten, removing the default CSS for the
Recent Comments widget:

More information about the wp-hackers mailing list