如何访问常量定义的外部类?

时间:2021-03-06 13:12:06

I have defined some constants eg:

我已经定义了一些常量,例如:

define('DB_HOSTNAME', 'localhost', true);
define('DB_USERNAME', 'root', true);
define('DB_PASSWORD', 'root', true);
define('DB_DATABASE', 'authtest', true);

now when I try to do this:

现在当我尝试这样做时:

class Auth{
function AuthClass() {
$this->db_link = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) 
or die(mysql_error());
}
}

I get an error. Why is this and what do I need to do?

我收到一个错误。为什么这是我需要做什么?

See, I've tried using (for example) global DB_HOSTNAME but this fails with an error.

请参阅,我已尝试使用(例如)全局DB_HOSTNAME,但此操作失败并显示错误。

The error I am getting is:

我得到的错误是:

Unknown MySQL server host 'DB_HOSTNAME' (1)

未知的MySQL服务器主机'DB_HOSTNAME'(1)

3 个解决方案

#1


11  

When the script runs, both the constant and the class definitions should be included.

脚本运行时,应包括常量和类定义。

e.g.

例如

constants.php.inc

constants.php.inc

define('DB_HOSTNAME', 'localhost', true);
define('DB_USERNAME', 'root', true);
define('DB_PASSWORD', 'root', true);
define('DB_DATABASE', 'authtest', true);

Auth.php.inc

Auth.php.inc

class Auth{
    function AuthClass() {
        $this->db_link = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) 
           or die(mysql_error());
    }
}

script.php

script.php的

include "constants.php.inc";
include "Auth.php.inc";

//do stuff

#2


2  

It should work as long as you've defined the constants before AuthClass() runs. If they are not in the same file as your Auth class, you will first need to include them within the file that Auth resides in so it can see them:

只要在AuthClass()运行之前定义了常量,它就应该工作。如果它们与Auth类不在同一个文件中,您首先需要将它们包含在Auth所在的文件中,以便它们可以看到它们:

include("the_file_that_has_those_constants_in_it.php");

Then it should just work. Constants are already global so there is no need to use the global keyword.

那它应该工作。常量已经是全局的,因此不需要使用global关键字。

#3


1  

It sounds like your constants aren't being defined before you instantiate class Auth. When you use an undefined constant this way, PHP will issue a warning and convert it to a string. If the problem is indeed that your constants aren't defined, your code will effectively be interpreted as:

听起来你在实例化类Auth之前没有定义常量。当您以这种方式使用未定义的常量时,PHP将发出警告并将其转换为字符串。如果确实问题确实没有定义常量,那么您的代码将被有效地解释为:

$this->db_link = mysql_connect('DB_HOSTNAME', 'DB_USERNAME', 'DB_PASSWORD');

Given the error you're getting (Unknown MySQL server host 'DB_HOSTNAME'), I'm assuming this is what's happening.

鉴于你得到的错误(未知的MySQL服务器主机'DB_HOSTNAME'),我假设这是正在发生的事情。

As other answers state, make sure you're defining the constants before you attempt to call Auth::AuthClass. If the class and the DB_* constants are defined in different files, make sure that both files are included before you attempt to instantiate/use class Auth.

正如其他答案所述,请确保在尝试调用Auth :: AuthClass之前定义常量。如果类和DB_ *常量在不同的文件中定义,请确保在尝试实例化/使用类Auth之前包含这两个文件。


As an aside, defined constants are not variables. You cannot use global CONSTANT_NAME; in this way, and you do not need to - all constants are always global, and available everywhere after the point they're defined. A simple test proves this:

另外,定义的常量不是变量。您无法使用全球CONSTANT_NAME;通过这种方式,您不需要 - 所有常量始终是全局的,并且在定义它们之后的任何地方都可用。一个简单的测试证明了这一点:

define ('MY_CONST', 3);

class Test {
    function __construct() { echo MY_CONST; }
}

$x = new Test(); // outputs 3

#1


11  

When the script runs, both the constant and the class definitions should be included.

脚本运行时,应包括常量和类定义。

e.g.

例如

constants.php.inc

constants.php.inc

define('DB_HOSTNAME', 'localhost', true);
define('DB_USERNAME', 'root', true);
define('DB_PASSWORD', 'root', true);
define('DB_DATABASE', 'authtest', true);

Auth.php.inc

Auth.php.inc

class Auth{
    function AuthClass() {
        $this->db_link = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) 
           or die(mysql_error());
    }
}

script.php

script.php的

include "constants.php.inc";
include "Auth.php.inc";

//do stuff

#2


2  

It should work as long as you've defined the constants before AuthClass() runs. If they are not in the same file as your Auth class, you will first need to include them within the file that Auth resides in so it can see them:

只要在AuthClass()运行之前定义了常量,它就应该工作。如果它们与Auth类不在同一个文件中,您首先需要将它们包含在Auth所在的文件中,以便它们可以看到它们:

include("the_file_that_has_those_constants_in_it.php");

Then it should just work. Constants are already global so there is no need to use the global keyword.

那它应该工作。常量已经是全局的,因此不需要使用global关键字。

#3


1  

It sounds like your constants aren't being defined before you instantiate class Auth. When you use an undefined constant this way, PHP will issue a warning and convert it to a string. If the problem is indeed that your constants aren't defined, your code will effectively be interpreted as:

听起来你在实例化类Auth之前没有定义常量。当您以这种方式使用未定义的常量时,PHP将发出警告并将其转换为字符串。如果确实问题确实没有定义常量,那么您的代码将被有效地解释为:

$this->db_link = mysql_connect('DB_HOSTNAME', 'DB_USERNAME', 'DB_PASSWORD');

Given the error you're getting (Unknown MySQL server host 'DB_HOSTNAME'), I'm assuming this is what's happening.

鉴于你得到的错误(未知的MySQL服务器主机'DB_HOSTNAME'),我假设这是正在发生的事情。

As other answers state, make sure you're defining the constants before you attempt to call Auth::AuthClass. If the class and the DB_* constants are defined in different files, make sure that both files are included before you attempt to instantiate/use class Auth.

正如其他答案所述,请确保在尝试调用Auth :: AuthClass之前定义常量。如果类和DB_ *常量在不同的文件中定义,请确保在尝试实例化/使用类Auth之前包含这两个文件。


As an aside, defined constants are not variables. You cannot use global CONSTANT_NAME; in this way, and you do not need to - all constants are always global, and available everywhere after the point they're defined. A simple test proves this:

另外,定义的常量不是变量。您无法使用全球CONSTANT_NAME;通过这种方式,您不需要 - 所有常量始终是全局的,并且在定义它们之后的任何地方都可用。一个简单的测试证明了这一点:

define ('MY_CONST', 3);

class Test {
    function __construct() { echo MY_CONST; }
}

$x = new Test(); // outputs 3