前端之家收集整理的这篇文章主要介绍了
php – 如何访问常量定义的外部类?,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经定义了一些常量,例如:
- define('DB_HOSTNAME','localhost',true);
- define('DB_USERNAME','root',true);
- define('DB_PASSWORD',true);
- define('DB_DATABASE','authtest',true);
现在当我尝试这样做:
- class Auth{
- function AuthClass() {
- $this->db_link = MysqL_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD)
- or die(MysqL_error());
- }
- }
我收到一个错误.为什么这是我需要做什么?
看到,我已经尝试使用(例如)全局DB_HOSTNAME,但是失败并出现错误.
我得到的错误是:
Unknown MysqL server host ‘DB_HOSTNAME’ (1)
当脚本运行时,应该
包括常量和类定义.
例如
constants.PHP.inc
- define('DB_HOSTNAME',true);
Auth.PHP.inc
- class Auth{
- function AuthClass() {
- $this->db_link = MysqL_connect(DB_HOSTNAME,DB_PASSWORD)
- or die(MysqL_error());
- }
- }
script.PHP的
- include "constants.PHP.inc";
- include "Auth.PHP.inc";
-
- //do stuff