Using add action for class

i have this i answer in reference but seems have upvoted by many https://wordpress.stackexchange.com/a/48094/145078

but not sure why it is not working for me..

in answer

class MyClass {
function __construct() {
add_action( ‘admin_init’,array( $this, ‘getStuffDone’ ) );
}
function getStuffDone() {
// .. This is where stuff gets done ..
}
}
$var = new MyClass();

don’t i have to set the visibility?

class MyClass {
public function __construct() {
add_action( ‘admin_init’,array( $this, ‘getStuffDone’ ) );
}
public function getStuffDone() {
// .. This is where stuff gets done ..
}
}
$var = new MyClass();

above code does not works for me, is not correct or any mistake from my side?

but this code works fine

add_action(‘init’,function(){
$lat = new NSClassopac();
$lath->getStuffDone();
});

Read more here:: Using add action for class

Leave a Reply

Your email address will not be published. Required fields are marked *