views:

32

answers:

2

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.

GF

A: 

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.

Ikke
Yes I thought I was simply injecting code into a class.Obviously you cannot.
+2  A: 

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.

Shein Alexey