Sample code:
function test_loaded() {
require_once 'file.php';
$ins = Sample_Class::instance();
require_once 'file2.php';
}
add_action('plugins_loaded', 'test_loaded');
in file.php
class Sample_Class {
public static $instance = null;
public static function instance() {
if(null == self::$instance) {
self::$instance = new Sample_Class();
}
return self::$instance;
}
public static function sample($a1) {
$a2 = get_option($a1);
return $a2;
}
}
in file2.php
$a3 = Sample_Class::instance();
function sample2() {
global $a3;
echo $a3->sample('test');
}
add_action('wp_head', 'sample2');
function sample3() {
global $a3;
echo $a3->sample('test2');
}
add_action('wp_footer', 'sample3');
Problem:
Sample2 expected output head section: get_option('test') -> this value But $a3 is output null (i use var_dump($a3) in function) No error appears on sample3 function side and outputs $ a3 is object. (i use var_dump())
Anyone have any ideas? Why am I having trouble using the wp_head hook.
My workaround is: To redefine $a3 inside the sample2 function instead of using global. It works this way. But I don't know why I should use it like that.
file2
you're trying to use global variables named$a3
but that variable doesn't exist, nowhere do you declare a global variable then create it. You should not use global variables to build your logic, or singletons with only static functions. The problem here has nothing to do with hooks