I'm writing a plugin which defines a class that is meant to be called multiple times. The class uses `add_action` to register it's own methods as callbacks.

    class MyClass {
    
        __construct($foo) {
    
            add_action('hook1', array($this, 'method1') );
            add_action('hook2', array($this, 'method2') );
    
        }
    
        public function method1() {...}
    
        public function method2() {...}
    
    }

This works just fine. But when I do it twice, only the second works.

    new MyClass('foo');
    new MyClass('bar');

In other words, the results of the code above are exactly what I'd expect from the following:

    // new MyClass('foo');
    new MyClass('bar');

It seems like the second instance overrides the first. What am I doing wrong?


As a sanity check, I tried this and it STILL doesn't work. The only difference is that now only the FIRST instance works. Now I'm totally confused.

    class MyClass1 {
    
        __construct($foo) {
    
            add_action('hook1', array($this, 'method1') );
            add_action('hook2', array($this, 'method2') );
    
        }
    
        public function method1() {...}
    
        public function method2() {...}
    
    }
    
    class MyClass2 {
    
        __construct($foo) {
    
            add_action('hook1', array($this, 'method1') );
            add_action('hook2', array($this, 'method2') );
    
        }
    
        public function method1() {...}
    
        public function method2() {...}
    
    }
    
    $foo = new MyClass1('foo');
    $bar - new MyClass2('bar');