dqc42632 2010-03-06 15:05
浏览 17
已采纳

在OO PHP类中使用“includes”

I have a class that inserts information into a MySQL database. I have the connection variables set as fields at the top of the class. When I run the class it works perfectly.

I would like to simply store the class fields in a separate file (not a class but just the fields saved in a file) and include it inside my class. when I do this though, it doesn't work. Why is this?

GF

Here's the Code:

class RegChecker{


  //set database connection fields
public $hostname_db_conn = "localhost";
public $database_db_conn = "thesis_db";
public $username_db_conn = "root";
public $password_db_conn = "";
......
}

I simply just want to take those four fields and store them in a simple php file and include it in place of them in the actual class e.g.

class RegChecker{


  //set database connection fields
include ("db_connection_fields");
......
}

This doesn't work however.

  • 写回答

2条回答 默认 最新

  • duandu2159 2010-03-06 15:29
    关注

    You can't use include statement inside class, only inside methods. You can do something like this

    class RegChecker {    
         function __construct() {
              $this->db_params = include 'db.conf.php';
         }
    }
    

    inside db.conf.php

    return array(
    'host' => 'localhost',
    'user' => 'root',
    'pass' => '',
    'db'   => 'thesis_db'
    );
    

    BTW, RegChecker is an awful name for class establishing connection to a database. Common practice to make separate Db class and pass this instance everywhere it needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决