admin管理员组文章数量:1127689
In front-page.php I have the following code:
<span class="amountOnline">
Online:
<?php echo $cbMain->GetInfo()['Players'] . '/' . $cbMain->GetInfo()['MaxPlayers']; ?>
</span>
Which is a function in functions.php that calls from this script.It works perfectly.
But once I move this code from front-page.php
to header.php
, I get the following error:'
Notice: Undefined variable: cbMain in C:\Users\...\header.php on line 102
Fatal error: Call to a member function GetInfo() on a non-object in C:\Users\...\header.php on line 102
In functions.php, I have this function for example:
function get_cbMain_Query() {
define( 'Main_SERVER_ADDR', '0.0.0.0');
define( 'Main_SERVER_PORT', 25565);
define( 'Main_TIMEOUT', 1 );
// require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';
require_once __DIR__ . '/includes/mcQuery/MinecraftQuery.class.php';
Error_Reporting( E_ALL | E_STRICT );
Ini_Set( 'display_errors', true );
$Timer = MicroTime( true );
$Query = new MinecraftQuery( );
try
{
$Query->Connect( Main_SERVER_ADDR, Main_SERVER_PORT, Main_TIMEOUT );
}
catch( MinecraftQueryException $e )
{
// $Error = $e->getMessage();
// echo 'error. <br>'. $Error;
}
return $Query;
}
Then
$cbMain = get_cbMain_Query();
So I can use functions in my static front-page.php file, but once I move this script to header.php, it does not work and gives an error. How do I fix this?
In front-page.php I have the following code:
<span class="amountOnline">
Online:
<?php echo $cbMain->GetInfo()['Players'] . '/' . $cbMain->GetInfo()['MaxPlayers']; ?>
</span>
Which is a function in functions.php that calls from this script.It works perfectly.
But once I move this code from front-page.php
to header.php
, I get the following error:'
Notice: Undefined variable: cbMain in C:\Users\...\header.php on line 102
Fatal error: Call to a member function GetInfo() on a non-object in C:\Users\...\header.php on line 102
In functions.php, I have this function for example:
function get_cbMain_Query() {
define( 'Main_SERVER_ADDR', '0.0.0.0');
define( 'Main_SERVER_PORT', 25565);
define( 'Main_TIMEOUT', 1 );
// require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';
require_once __DIR__ . '/includes/mcQuery/MinecraftQuery.class.php';
Error_Reporting( E_ALL | E_STRICT );
Ini_Set( 'display_errors', true );
$Timer = MicroTime( true );
$Query = new MinecraftQuery( );
try
{
$Query->Connect( Main_SERVER_ADDR, Main_SERVER_PORT, Main_TIMEOUT );
}
catch( MinecraftQueryException $e )
{
// $Error = $e->getMessage();
// echo 'error. <br>'. $Error;
}
return $Query;
}
Then
$cbMain = get_cbMain_Query();
So I can use functions in my static front-page.php file, but once I move this script to header.php, it does not work and gives an error. How do I fix this?
Share Improve this question edited Jan 22, 2013 at 6:39 devs asked Jan 22, 2013 at 5:33 devsdevs 1131 gold badge3 silver badges9 bronze badges 1- this has nothing to do with WordPress, it's a generic PHP problem, namely one of scope. You can't declare variables in one scope then use them in another without a connecting action of some kind such as a declaring in both places that it's a global variable or a step to retrieve and set the variable when it's used – Tom J Nowell ♦ Commented Jan 9, 2024 at 17:48
2 Answers
Reset to default 1The content of your header.php
is called with get_header()
, so all variales inside of this file act in another scope than the calling file.
So either get the variable from the global scope or build a wrapper in your functions.php
to access the object from anywhere:
function get_cbmain()
{
static $instance = NULL;
NULL === $instance and $instance = new MinecraftQuery;
return $instance;
}
Now you can get the object instance with:
get_cbmain()->GetInfo();
Your class instance is not in the scope of header.php
, you need global $cbMain
before accessing member functions.
本文标签: static websitecan not call functions in functionphp
版权声明:本文标题:static website - can not call functions in function.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736697911a1948279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论