0

Possible Duplicate:
Reference - What does this error mean in PHP?

In the following code, I created two functions that needs the same information. I'm using this script.

<?php 
function get_cbMain_Query() {
  define( 'Main_SERVER_ADDR', 'ip');
  define( 'Main_SERVER_PORT', 25565);
  define( 'Main_TIMEOUT', 1 );

  // require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';
  require __DIR__ . '/mcQuery/MinecraftQuery.class.php';

  // Display everything in browser, because some people can't look in logs for errors
  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;
}

function get_cbTekkit_Query() {
  define( 'Tekkit_SERVER_ADDR', 'ip');
  define( 'Tekkit_SERVER_PORT', 25565);
  define( 'Tekkit_TIMEOUT', 1 );

  // require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';
  require __DIR__ . '/mcQuery/MinecraftQuery.class.php';

  // Display everything in browser, because some people can't look in logs for errors
  Error_Reporting( E_ALL | E_STRICT );
  Ini_Set( 'display_errors', true );

  $Timer = MicroTime( true );
  $Query = new MinecraftQuery( );

  try
  {
    $Query->Connect( Tekkit_SERVER_ADDR, Tekkit_SERVER_PORT, Tekkit_TIMEOUT );
  }
  catch( MinecraftQueryException $e )
  {
    $Error = $e->getMessage();
   echo 'error. <br>'. $Error;
  }
  return $Query;
}

When using this script and do the following to call it (From a different page, this script is included in another)

          $cbMain = get_cbMain_Query();
          $cbTekkit = get_cbTekkit_Query();

Then I get:

 Cannot redeclare class MinecraftQueryException in MinecraftQuery.class.php on line 5

I am new to classes ad objects in PHP and can't figure out why i can't call two functions and use them at the same time. Help please?

Community
  • 1
  • 1
devs
  • 541
  • 2
  • 13
  • 27

2 Answers2

3

Change the line in both functions

require __DIR__ . '/mcQuery/MinecraftQuery.class.php';

to read

require_once __DIR__ . '/mcQuery/MinecraftQuery.class.php';

This will ensure the file is included once, and avoid the error.

Kami
  • 19,134
  • 4
  • 51
  • 63
0

you either do require_once or include_once or you can put an

if(!function_exists('get_cbMain_Query')){... }
if(!function_exists('get_cbTekkit_Query')){... }

around each function to make sure they are only declared once.

Ian Overton
  • 1,060
  • 7
  • 17