Darkmatter5 Posted May 28, 2009 Share Posted May 28, 2009 config.php is in "public_html/library" funcs.php is in "public_html/library" index.php is in "public_html" Here's config.php <?php $admin_email="****"; $url="http://www.mysite.com"; $dbhost="localhost"; $dbuser="user"; $dbpass="pass"; $dbname="name"; ?> Here's funcs.php <?php class werb { function page_theme() { include_once('config.php'); echo "dbhost: $dbhost"; } } ?> Here's index.php <?php include('library/config.php'); require_once('library/funcs.php'); $werb=new werb(); $werb->page_theme(); ?> All I get from loading index.php is: dbhost: It should be: dbhost: localhost What's wrong? Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/ Share on other sites More sharing options...
paulman888888 Posted May 28, 2009 Share Posted May 28, 2009 You need to get GLOBAL variables <?php class werb { function page_theme() { include_once('config.php'); echo "dbhost: $dbhost"; } } ?> should be <?php class werb { function page_theme() { global $dbhost; include_once('config.php'); echo "dbhost: $dbhost"; } } ?> Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844441 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Try include instead of include_once. Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844454 Share on other sites More sharing options...
corbin Posted May 28, 2009 Share Posted May 28, 2009 Why would you even need to know your database params in a theme method? Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844477 Share on other sites More sharing options...
roopurt18 Posted May 28, 2009 Share Posted May 28, 2009 Perhaps the user's theme is stored in their database profile? You're going to get confused with all those variables floating around the global namespace. You'd be better off creating a singleton class called MyConfig that reads your config.php file and loads members with the config values. Also, you might as well make your config.php a config.ini file and just use PHP's parse_ini_file() function. /* index.php */ require_once( 'classes/MyConfig.php' ); include( 'somefile.php' ); /* somefile.php */ function a_random_func() { $cfg = MyConfig::getInstance(); echo 'dbhost: ' . $cfg->dbhost . "\n"; } Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844479 Share on other sites More sharing options...
corbin Posted May 28, 2009 Share Posted May 28, 2009 Perhaps the user's theme is stored in their database profile? Yes, but shouldn't the connection already have been made? Why would a page_theme method be responsible for creating a MySQL connection? Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844484 Share on other sites More sharing options...
roopurt18 Posted May 28, 2009 Share Posted May 28, 2009 Probably. But since he's learning... Consider setting up the MyConfig singleton as I suggested. I also like to use a Database singleton that returns a handle to a PDO object. Then I can just do: <?php $pdo = Database::getPDO(); ?> <?php /* Database.php */ class Database { static private $_pdo = false; static public function getPDO() { if( self::$_pdo === null ) { $cfg = MyConfig::getInstance(); $pdo = new PDO( /* fill this in */ ); if( $pdo instanceof PDO ) { self::$_pdo = $pdo; }else{ throw new Exception( "PDO creation." ); } } return self::$_pdo; } } ?> Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844510 Share on other sites More sharing options...
corbin Posted May 28, 2009 Share Posted May 28, 2009 Probably. But since he's learning... Eh true.... Link to comment https://forums.phpfreaks.com/topic/160067-help-with-including-a-file/#findComment-844516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.