Why I get NULL object after initialization WordPress plugin?

I have written a custom plugin in WordPress.

This is simple class:

class Post {

    public  $replacement_id;
    private $replacement_content;
    private $post;

    public function __construct(){
        add_action('the_post', array($this, 'post_callback'));
    }

    public function post_callback($post) {
        $this->replacement_id = $post->id_content_replacement;
        $this->post = $post;
    }

    public function replaceContent(){
        $this->replacement_content = & get_post($this->replacement_id);
        $this->post = $this->replacement_content;
    }

    public function _view(){
        return $this->post;
    }
}

// Show Post
$postObject = new Post();

In method post_callback I get data from DB:

$this->replacement_id = $post->id_content_replacement; // Takes value 5

Variable $this->replacement_id keeps value 5.

In the end I create exemplar of class:

$postObject = new Post();

And try to get value of public property:

$replacement_id

But I get NULL instead 5 Why?

Read more here: Why I get NULL object after initialization WordPress plugin?

Leave a Reply

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