WordPress class, using add_action to call member function does not work

Well it can’t and won’t work like you want it to.

Let’s look at what exactly are you doing.

Inside your constructor you have:

add_action('init', array( &$this, 'init' ) ); //this doesn't work

So you add init method of your object to WPs init hook. When WP runs it’s init action, then your init method will be run to, but not sooner and not later.

Then you do something like this:

$learnObj =  new learnable_test();

$learnObj->printAge();

So you create object of your class. It will add $learnObj->init to WPs init hook.

If you call these 2 lines after WPs init hook was already done, nothing will happen.

If you call them before (I guess you do) – your $learnObj->init will be executed during WPs init action (and age variable will be set).

But in the second line you want to access this age variable. It won’t be set here yet, because WP haven’t yet executed it’s init action, thus your $learnObj->init hasn’t been executed too.