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

前端之家收集整理的这篇文章主要介绍了php – 如何访问常量定义的外部类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经定义了一些常量,例如:
  1. define('DB_HOSTNAME','localhost',true);
  2. define('DB_USERNAME','root',true);
  3. define('DB_PASSWORD',true);
  4. define('DB_DATABASE','authtest',true);

现在当我尝试这样做:

  1. class Auth{
  2. function AuthClass() {
  3. $this->db_link = MysqL_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD)
  4. or die(MysqL_error());
  5. }
  6. }

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

看到,我已经尝试使用(例如)全局DB_HOSTNAME,但是失败并出现错误.

我得到的错误是:

Unknown MysqL server host ‘DB_HOSTNAME’ (1)

当脚本运行时,应该包括常量和类定义.

例如

constants.PHP.inc

  1. define('DB_HOSTNAME',true);

Auth.PHP.inc

  1. class Auth{
  2. function AuthClass() {
  3. $this->db_link = MysqL_connect(DB_HOSTNAME,DB_PASSWORD)
  4. or die(MysqL_error());
  5. }
  6. }

script.PHP

  1. include "constants.PHP.inc";
  2. include "Auth.PHP.inc";
  3.  
  4. //do stuff

猜你在找的PHP相关文章