0

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.

1
  • 1
    In 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
    – Tom J Nowell
    Commented Sep 21, 2020 at 22:19

1 Answer 1

0

As an alternative you could try declaring it with $GLOBALS['a3'] in file2 to make sure it becomes a global:

$GLOBALS['a3'] = Sample_Class::instance();

I'd guess the current $a3 declaration in file2's top level scope doesn't count as global because it gets inluded inside the test_loaded() scope. From the 'include' docs (which cover require and require_once too):

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

So it looks like variables declared at file scope in an included file are an exception to true global scope. However I'm surprised there's not an equivalent exception to allow them to be accessed as globals from file2 anyway, since that changes how $a3 behaves depending on how the file can be included.


If you don't need $a3 to be global outside file2, another option might be to change sample2() into an anonymous closure that includes $a3 with a 'use':

$a3 = Sample_Class::instance();

add_action( 'wp_head', function() use ($a3) {
    echo $a3->sample('test');
});
1
  • Using GLOBAL ['a3'] solves the problem, yes you are right. As you said, it is not specified globally because it is defined in test_loaded. But another point I want to mention is the sample3 function. So how does the global variable work there? Thanks for everything though!
    – Akın Ocak
    Commented Sep 22, 2020 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.