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
Add a comment  | 

2 Answers 2

Reset to default 1

The 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