Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.