Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Wordpress Functions.PHP获取类的实例(header.PHP中未定义变量)_Php_Wordpress_Oop - Fatal编程技术网

PHP Wordpress Functions.PHP获取类的实例(header.PHP中未定义变量)

PHP Wordpress Functions.PHP获取类的实例(header.PHP中未定义变量),php,wordpress,oop,Php,Wordpress,Oop,functions.php final class Test { public function helloworld() { return "Hello World!"; } } $testy = new Test; 当我尝试调用$testy->helloworld()时;在header.php文件中,它确实给了我一个错误,比如变量未定义 当我在我的页眉php中调用实例$testy时,就像这样,然后将该实例与函数helloworld()一起使用,一切正

functions.php

final class Test {

     public function helloworld() {
       return "Hello World!";
     }

}

$testy = new Test;
当我尝试调用$testy->helloworld()时;在header.php文件中,它确实给了我一个错误,比如变量未定义

当我在我的页眉php中调用实例$testy时,就像这样
,然后将该实例与函数helloworld()一起使用,一切正常,但我需要在页脚中再次调用它,以使该实例也适用于我的页脚

因此,我想知道如何在functions.php中编写它,而不在header.php和footer.php中声明它

希望有人能帮我。

你可以用
final class Test {

     public function helloworld() {
       return "Hello World!";
     }

}

$testy = new Test;

$_GLOBAL['testy'] = $testy;


?>

还有其他方法吗?我如何使用$\u全局变量选择函数helloworld()?