TLaude Posted August 30, 2009 Share Posted August 30, 2009 Here is what I'm trying to accomplish. I need a file (const_values.php) to store primary values that will allow me to use them all over my website while having the values originate in 1 location so if the value ever changes, I can just change it in the 1 spot. Then, once const_values.php is set up, how would I go about calling it into my functions.php page? Here is what I am thinking, but I'm not sure how well it is going to work. // const_values.php <?php class const_values { function const_values() { define(DB_HOST, '***'); define(DB_USER, '***'); define(DB_PASS, '***'); } } ?> <?php // functions.php include('const_values.php') var $dbhost; var $dbuser; var $dbpass; $this->dbhost = const_values->DB_HOST; $this->dbuser = const_values->DB_USER; $this->dbpass = const_values->DB_PASS; class database { functions dbConnect() { mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) } } ?> Am I close or am I far off? Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/ Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 class Configuration { const DB_HOST = 'host'; const DB_USERNAME = 'username'; const DB_PASSWORD = 'password'; const DB_NAME = 'name'; } class Database { protected $_host = null; protected $_username = null; protected $_password = null; protected $_name = null; public function __construct() { $this->_host = Configuration::DB_HOST; $this->_username = Configuration::DB_USERNAME; $this->_password = Configuration::DB_PASSWORD; $this->_name = Configuration::DB_NAME; } } Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909273 Share on other sites More sharing options...
TLaude Posted August 30, 2009 Author Share Posted August 30, 2009 class Configuration { const DB_HOST = 'host'; const DB_USERNAME = 'username'; const DB_PASSWORD = 'password'; const DB_NAME = 'name'; } class Database { protected $_host = null; protected $_username = null; protected $_password = null; protected $_name = null; public function __construct() { $this->_host = Configuration::DB_HOST; $this->_username = Configuration::DB_USERNAME; $this->_password = Configuration::DB_PASSWORD; $this->_name = Configuration::DB_NAME; } } I need them in DIFFERENT files. Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909310 Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 class Configuration { const DB_HOST = 'host'; const DB_USERNAME = 'username'; const DB_PASSWORD = 'password'; const DB_NAME = 'name'; } class Database { protected $_host = null; protected $_username = null; protected $_password = null; protected $_name = null; public function __construct() { $this->_host = Configuration::DB_HOST; $this->_username = Configuration::DB_USERNAME; $this->_password = Configuration::DB_PASSWORD; $this->_name = Configuration::DB_NAME; } } I need them in DIFFERENT files. No problemo. Create a file class.configuration.php and a file class.database.php. In your class.database.php add the line and in every other file you need the configuration constants: require_once('class.configuration.php'); Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909311 Share on other sites More sharing options...
TLaude Posted August 30, 2009 Author Share Posted August 30, 2009 Ok, that is what I thought. I've been trying that and can't seem to get it to work. I'll play around with it some more. Thanks ignace! Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909315 Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 Ok, that is what I thought. I've been trying that and can't seem to get it to work. I'll play around with it some more. Thanks ignace! require_once('class.configuration.php'); class Database { Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909326 Share on other sites More sharing options...
RussellReal Posted August 30, 2009 Share Posted August 30, 2009 the reason ignace's example would work is because you are trying to go into an instance, where as he is looking inside the class model, also you used define() which would go into the global scope rather then the object's scope, also.. You're defining it inside a function which requires a process to define.. which than isn't findable within the original class model. :: looks into the original model of the class.. -> looks into an instance of that class Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909367 Share on other sites More sharing options...
TLaude Posted August 30, 2009 Author Share Posted August 30, 2009 require_once('class.configuration.php'); Whats the difference between this and the following: require_once('configuration.php'); I'm assuming the first one will pull in the variables from the class itself? Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909408 Share on other sites More sharing options...
RussellReal Posted August 31, 2009 Share Posted August 31, 2009 no... just programmers seperate regular php files.. from class or library php files.. if you're making the php file JUST for a class.. for your own future ease.. you'd name it XXXX.class.php or class.xxxx.php or w.e and some peopl ename it lib.xxx.php or xxx.lib.php for libraries of functions.. its just a programming method Quote Link to comment https://forums.phpfreaks.com/topic/172472-oop-constants/#findComment-909435 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.