开发者

Using "includes" in an OO PHP Class

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 s开发者_如何学编程aved 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.


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.


You are inserting a class into a class. This does not work in PHP.

I'm trying to figure out if you can inject code into a class in PHP this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜