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.
Related Posts:
- Call to undefined function is_home() or any conditional tags
- Adding tables to dashboard pages programmatically?
- Difference Between Filter and Action Hooks?
- How to create an API for my plugin?
- Using OOP plugin’s methods throughout the website
- How do you use the plugin boilerplate loader class to hook actions and filters?
- add_filter OO with parameters
- How Can I Add a Filter to Class Instance Only?
- How do I Make a Theme “plugin-ready”?
- function triggered by “manage_users_custom_column” filter not working
- Actions or filters fired when data is saved in a custom table
- Determine which theme location a wp_get_nav_menu_items is for
- What’s the difference between hooks, filters and actions? [duplicate]
- How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’
- Is it possible to create an action hook using do_action() within add_action()?
- Dynamically Override Fancy Title
- What is the Difference between directly call a function and call a function using add_action?
- How to add custom content under plugin row in WordPress admin plugin list?
- Define a function outside a class and call the function using action or filter hook
- Does add_filter work outside functions.php
- Woocommerce – Hide a Column in Cart Table
- WordPress after content Hook & external template part
- Namespaced action and filter tags
- get_current_screen() return null
- Adding rewrite rule dynamically
- Prefixing plugin hooks (actions/filters) with a wrapper class or functions
- How to filter content for specific content variable
- Valid filenames for add_action’s first parameter
- How to get all queries’s results after they have executed?
- Remove an action by extending class and replacing it
- A question about add_action()
- Hook add_attachment error
- How to find list of all functions bind to a particular hook from my plugin?
- Include HTML (Bootstrap Modal Box) with a plugin into my header
- Setting post_id for single.php based on URL without a redirect
- apply_filters() and call_user_func() to define and call a function outside a class
- Override category archive page title (not the head title)
- WordPress Plugin Boilerplate – add actions and/or filters based on user’s role
- wp_ajax add_action fuction won’t fire on custom jQuery action
- WordPress Reset password Strength set to medium
- WordPress permalink setting
- Apply function on all action hooks?
- Ninja Forms: Front-End Forms, Post ID?
- function add custom fields to media gallery
- Bad request 400 using class based files
- Limit get_next_post to posts from the same author
- Where do I hook to have the server do something in PHP on block attribute change?
- The function called on the wp head hook becomes null
- Save_post – Warning: Cannot modify header information
- Make visible page only in the trash
- Redirection from a specific page for users logged in but not with membership
- WordPress: I wanted to use Add rewrite rule multiple times for multiple CPT in my plugin
- Override woocommerce loop-start.php from theme using plugin?
- post value to function with Ajax and jQuery
- How Do I Unhook This Parent Theme Function?
- Any way, hook to add content right before the “read more” link?
- Why would one specify multiple arguments when using get_previous_posts_link() which takes only one parameter?
- I cant add is_admin control to plugin
- WordPress Ajax not returning Response
- ajax-action.php can’t find added action
- How to Unhook actions/filters in within Class in plugin
- functions won’t fire after I converted my code from procedural code to OOP
- Post is uncatagorized in publish_post action
- Nested Actions and Filters
- Use action, filter, or hook to append HTML to WordPress plugin function
- Does hooking into a plugin action / filter override the plugin’s function for that hook?
- Get post content inside plugin class method
- Performing ajax request in wordpress
- do_action from within a class does not seem to work
- Error when activating a custom plugin on production server
- Remove tag from all posts when publishing new post
- WordPress plugin how to run function when button is clicked
- Remove action added in plugin class from theme
- OOP development and hooks
- How to set a public variable in someone else’s plugin class
- Redirect theme directory to plugin theme directory
- remove different admin menu for specific users
- Change Front page displays settings conditionally when user is online
- login_message filter not working
- Add_action not calling callback function
- Which method is more correct for removing WooCommerce Extensions menu item?
- How to Maintain url on form submit
- I have a plugin where in I have to change the title of the page dynamically by sending parameter to the_title filter
- WordPress Dropdown image select
- Add function to Dokan ajax class
- Modify search form with plugin
- How is construct function working even when variable is assigned null value?
- Single_template for a custom post type created from a plugin is returning an empty page
- foreach argument to get specific file types getting too many returns
- get_comment_meta() for a filter hook in a plugin
- add_option_{$option} action hook not being called
- WordPress Add advertising ads befor and after content with periority [closed]
- Which filter to use to pre-parse form elements, which are generated by plugin’s shortcode?
- Remove from a div by class name from post page if post author role is not administrator
- Function not working on any file other than the main plugin file
- Building WordPress Themes With Bootstrap with Adi Purdila → TutsPlus
- Custom signature appears twice on page
- Caption Shortcode: what filter to change the image size?
- Confusing $tag specification for apply_filters call, in core options.php code
- Does WordPress have something like timer hook?