scottybwoy Posted September 29, 2006 Share Posted September 29, 2006 I'm using OOP concepts in php5.I have a main parent class, but above it i have all my constants and configs included in a seperate file like so :[code]<?phprequire_once 'config.inc.php';class PHPApplication { ....}?>[/code]Later on in the file a call to another file occurs to display the page. Within the called file is another class that extends the original but all my configs are gone. Is there a better way to have my configs included once and be available to all my files?Thanks Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 29, 2006 Share Posted September 29, 2006 Sounds more like a scope issue than to do with the actual includes.Could you show an example of a few of your config settings? Are they variables, constants or such? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted September 29, 2006 Author Share Posted September 29, 2006 constants mainly and some variables, but I don't think my path is being set.Here's the config file :[code]<?php define('APP_DB', 'mri_sql'); define('AUTH_DB_TBL', 'users'); define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']); define('INTRANET_DIR', ROOT_PATH . '/database'); define('SCRIPTS', INTRANET_DIR . '/scripts'); define('CLASSES', INTRANET_DIR . '/classes'); define('LIBS', INTRANET_DIR . '/library'); define('PHPLIB_DIR', LIBS . '/php'); define('JS_LIB_PATH', LIBS . '/js'); define('PATH', CLASSES . ';' . SCRIPTS . ';' . LIBS); ini_set( 'include_path', ';' . PATH . ';' . ini_get('include_path')); define('TEMPLATE_DIR', INTRANET_DIR . '/templates'); require_once 'constants.php'; require_once $DEBUGGER_CLASS;// require_once $ERROR_HANDLER_CLASS;// require_once $USER_CLASS;// require_once $TEMPLATE_CLASS;// And my database connection?>[/code]But I just wanted to know that if was included once already shoud it be available accross the board? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted September 29, 2006 Author Share Posted September 29, 2006 As an added query, to above. If I have a function __construct() in my initial class, will it be run every time a child class extends it? And is there a way of counting how many times a class is included/run? Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 29, 2006 Share Posted September 29, 2006 you have to specify parent::__construct() if you want child classes to use the parent's constrcutor.[code]<?phpclass A extends B{ public function __construct() { parent::__construct(); }}?>[/code]as for the include, it depends where you include it.Providing you include it pre-object instantiation you'll be ok.[code]<?phprequire_once 'config.php';$object = new MyClass($config);?>[/code]but what I mean by scope, is that if you declare a variable and try to use within a class (without global keyword, or passing as a parameter) it will not access it.[code]<?phpclass A{ private $foo; public function __construct() { $this->foo = $foo; // will not work. }}$foo = 'bar';$obj = new A;?>[/code][/code] Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted October 3, 2006 Author Share Posted October 3, 2006 Cheers Jenk, I think I've been following those rules, I just need to decipher now why my __consruct() is being run twice. Any ideas as to what may be the cause. I don't specifically call that function anywhere. Just run NewClass = new Class; And have another file being called that extends Class anything else do you know what what could do it? Quote Link to comment 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.