Could be a bug: PHP5 + Define + Class

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Could be a bug: PHP5 + Define + Class

Postby ElvansX » 13. December 2007 09:49

I try google for solution but can't find it anywhere. Thought this could be a bug, so decide to post this.

My problem is my web script doesnt work using DEFINE when call inside the class. Im using XAMPP Windows 1.6.4 installler. I got the problem after install xampp, copy my script and database to correct directory. Run for first time

This is sample what in my script: config.php
Code: Select all
  define("DB_SERVER","localhost");
  define("DB_USER","user");
  define("DB_PASS","password");
  define("DB_NAME","database");
  /// script continue... //



database.php
Code: Select all
include('config.php');

class MySQL {
  function MySQL() {
    mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());

  }
 
  /// script continue... //
}


The error only said : Unknown MySQL server host 'DB_SERVER' (11001)

I uninstall XAMPP Windows 1.6.4, and download XAMPP Lite 1.6.4, and it works just fine. There no way to disable DEFINE in php, but this is just the problem that confused me why DEFINE is not working in my script. It should works on PHP 4 and PHP5, tested when i install all package manually.

I have no time to figure it more what would cause the problem, better let the expert who know about xampp do about it
ElvansX
 
Posts: 1
Joined: 13. December 2007 09:31

Postby Scory » 13. December 2007 12:23

It's very confusing that you dont get a warning (whats the errorlevel?), because if define really does not work for constants inside the class object, you *should* get first a notice "Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in ...".

Try to find out if this is the case.

I played around a little and it depends on WHEN a new class object is created. The problem ist NOT the declaration of the class, but the moment, where you need the constants. Two different scripts, the first runs fine, the second does not. I think, it's not a bug, it works as designed:

No Error Script:
Code: Select all
<?php

define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");
 
 
$a = new MySQL();

class MySQL {
  function MySQL() {
    mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());

  }
}
?>


Error Script:
Code: Select all
<?php

$a = new MySQL();

define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");
 
class MySQL {
  function MySQL() {
    mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());

  }
}
?>


Another No Error Script:

Code: Select all
<?php

class MySQL {
  function MySQL() {
    mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());

  }
}

define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");

$a = new MySQL();
?>


Another Error Script:
Code: Select all
<?php

class MySQL {
  function MySQL() {
    mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());

  }
}

$a = new MySQL();

define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");

?>


You see, everything depends on WHEN you create a new instance of your object. It does NOT depend on WHEN you DECLARE the class. The declaration of a Constant via define() MUST be before you use that constant. A class declaration does not use the Constant, it is only used, when you create an instance of this class.

This is definately quite logical and i think it's ok, as a call to define() is a normal function call and as PHP is a procedural language, the resultung constant does NOT exist before that call.

Maybe it worked different in older versions of PHP, but PHP5 has a complete new redesign of object classes and i think, it's more logical now.

Greets
Scory
Scory
 


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 143 guests