admin管理员组文章数量:1429658
I am trying to do the following
functions.php
class Test {
public function hello(){
return 'Hello';
}
}
$test = new Test();
header.php or any other include
echo $test->hello();
But it is not accessible unless I define it as:
global $test;
echo $test->hello();
I really do not want this as I will end up with many variables to define. Is there anyway to make class object and methods being accessible from any include?
Thank you so much.
UPDATE, POSSIBLE SOLUTION
At the end I did the following:
functions.php
Included the class with require.
Added a function that returns instantiation of specific class
function test(){
return new Test();
}
And then it's methods become accessible from anywhere: includes, templates just like this:
header.php or any custom include or template
echo test()->hello();
That does work and I do not need to use $GLOBALS, I just hope I am not doing something awkward in terms of WordPress.
Would you approve that?
本文标签: functionsHow to access custom class methods from any include without using global
版权声明:本文标题:functions - How to access custom class methods from any include without using global 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745539288a2662417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论