phil88 Posted January 27, 2008 Share Posted January 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/88062-allowing-included-vars-to-be-used-by-classes/ Share on other sites More sharing options...
Daniel0 Posted January 27, 2008 Share Posted January 27, 2008 class database{ function connect($db){ mysql_connect($db['host'], $db['username'], $db['password']); } } Now pass $db to that method. Quote Link to comment https://forums.phpfreaks.com/topic/88062-allowing-included-vars-to-be-used-by-classes/#findComment-450568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.