Extend a class plugin

I can’t get the user variable from the main class loading a “child class” example:

//PLUGIN FILE
class father{

    var $user;

    function __construct() {
        add_action('plugins_loaded', array(&$this, 'loaded'));
    }

    function plugins_loaded(){
        global $wp_get_current_user;
        $this->user = wp_get_current_user();
    }
}

$plugin = new parent();

That was the plugin file.

Read More
//EXTEND CLASS
class child extends father{

    function __construct(){
        parent::__construct();
    }

    function user_id(){
        echo $this->user->ID;
    }
}

That was the extend class.

//CONFIG FILE (DISPLAYED IN ADMIN PANEL)
$child = new child();
$user_id = $child->user->id;
$child->user_id();

And that was the config page.

I can’t get the user id in the extended class, but yes in the father class.

Why and how i can solve it?

Related posts

Leave a Reply

1 comment

  1. This works for me:

    class father {
        var $user;
        function __construct() {
            add_action( 'init', array( &$this, 'set_user' ) );
        }
        function set_user() {
            $this->user = wp_get_current_user();
        }
    }
    
    class child extends father {
        function __construct() {
            parent::__construct();
        }
        function user_id(){
            return $this->user->ID;
        }
    }
    
    $father = new father();
    $child = new child();
    
    add_action( 'admin_notices', 'test_stuff' );
    function test_stuff() {
        global $child;
        print '<pre>Child: ' . print_r( $child->user_id(), true ) . '</pre>';
    }