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 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.