Jump to content

Allowing included vars to be used by classes


phil88

Recommended Posts

I have two files, one file (settings.php) that contains all sorts of misc. variables that are to be used by other scripts. The other file contains all the classes that are to be included.

 

//settings.php
$db['host'] = "localhost";
$db['username'] = "phil";
$db['password'] = "xxx";
etc.

// classes.php
include('settings.php');
class database{
    function connect(){
        mysql_connect($db['host'], $db['username'], $db['password']);
    }
}

A simplified example, but it shows the problem I'm having.

 

How can I make it so that the connect method of the database class can access the variables that were included at the top of the file? Eg. the $db array.

 

I've established that moving the include('settings.php'); line to be inside each of the methods that need the data from settings.php will work. But is there a better way so there is only 1 inclusion?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.