• Resolved umchal

    (@umchal)


    Hi,

    I just came across an issue that added callbacks with add_action() gets called twice, which I did not expect. I’m not sure whether this is an expected behavior or not.

    
    class Tester {
        public function tester() {
            error_log( __METHOD__ . ' called' );
        }
        public function testSomething() {
            error_log( __METHOD__ . ' called' );
        }    
    } 
    $_oTest1 = new Tester;
    add_action( 'plugins_loaded', array( $_oTest1, 'tester' ) ); 
    add_action( 'plugins_loaded', array( $_oTest1, 'testSomething' ) ); 
    

    You’ll see the method tester() gets called twice while testSomething() is called only once.

    Is this a bug or by design?

    • This topic was modified 7 years, 6 months ago by umchal. Reason: fixed mark-down
Viewing 2 replies - 1 through 2 (of 2 total)
  • In PHP if a class has a method of the same name it is treated as the constructor. This is from older versions, and has been deprecated, but it’s what will happen if your class does not have a __construct() method.

    For backwards compatibility with PHP 3 and 4, if PHP cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.

    http://www.php.net/manual/en/language.oop5.decon.php

    So tester() is being run when you do this: $_oTest1 = new Tester;

    Thread Starter umchal

    (@umchal)

    Oh, I see. Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Callback called twice when method name and class name is same’ is closed to new replies.