Instead of this
public function pcieh_shortcodes_init() {
$data = $this->data;
var_dump($data); //Return an array which is Expected
function pcieh_shortcode($atts = [], $content = null) {
global $data;
var_dump($data); //Return NULL
}
add_shortcode('pcieh', 'pcieh_shortcode');
}
Why not this
public function pcieh_shortcodes_init() {
$data = $this->data;
var_dump($data); //Return an array which is Expected
add_shortcode('pcieh', [$this,'pcieh_shortcode']);
}
public function pcieh_shortcode($atts = [], $content = null) {
var_dump($this->data); //Return NULL
}
Then there is no need for global
or any of the ugliness associated with it. It's perfectly acceptable to pass a class and method in as an array.
Basically wordpress is using call_user_func
to call this anyway.
http://php.net/manual/en/function.call-user-func.php
UPDATE
I should have noted, in the original code in the question, even if you did get it to work, you have no reference to the actual instance of this class, so any value you set in data (after assign the short code, and global) would have likely been lost anyway. In other words you would lose the state of the current object and any data it contains.
So by using the actual instance $this
you can be sure you are actually dealing with the instance you want to.