ifubad Posted July 28, 2009 Share Posted July 28, 2009 Need a little explanation. Read this snippet in a book. Exactly what is the author suggesting, storing all the constants in what type of file? Thanks in advance Were you to develop the MySQL classes further, you might end up with an unwieldy number of constants. In that case it would make sense to remove constant data members from their respective classes and store them in a file associated with the MySQLException class, Quote Link to comment https://forums.phpfreaks.com/topic/167732-solved-storing-lots-of-constants-outside-the-class/ Share on other sites More sharing options...
xtopolis Posted July 28, 2009 Share Posted July 28, 2009 You're going to have to be a little more specific, but the author was probably referring to a "config" (type) file. The concept behind this is a centralized point to store variables that contain some globally pertinent information, but not having to change the variable across multiple files. An example of this would be to store database configuration info (host,name,pass,dbname) in a file, then include the file when it's needed, rather than rewriting the database connectivity each time you need in in another script... (not the best example). Quote Link to comment https://forums.phpfreaks.com/topic/167732-solved-storing-lots-of-constants-outside-the-class/#findComment-884532 Share on other sites More sharing options...
ifubad Posted July 28, 2009 Author Share Posted July 28, 2009 The concept behind this is a centralized point to store variables that contain some globally pertinent information, but not having to change the variable across multiple files. Ahhh, either an include or ini file, kinda like below(of course the Constants class would actually be in a different file). I guess one can either use a class as below to store the constants, or a simple include with constants created by define(). Which method of storing constants would be the preferred/correct way out of these two, using const in a class or the procedural define()? For a medium to large site, would storing lots of constants and other pertinent data in an ini file be better than an include? <?php class Constants{ const QUANTITY = 1; const EXAMPLE_CONST = "Blah blah blah"; } class ExtException extends Exception{ private $msg = "Gummy Bear"; public function __construct(){ $obj = new Constants(); $msg = Constants::QUANTITY . " " . $this->msg; parent::__construct($msg); } } try { throw new ExtException(); } catch(ExtException $e) { echo $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167732-solved-storing-lots-of-constants-outside-the-class/#findComment-884577 Share on other sites More sharing options...
Daniel0 Posted July 28, 2009 Share Posted July 28, 2009 Storing a lot of constants usually wouldn't be a good idea regardless. Quote Link to comment https://forums.phpfreaks.com/topic/167732-solved-storing-lots-of-constants-outside-the-class/#findComment-884641 Share on other sites More sharing options...
xtopolis Posted July 28, 2009 Share Posted July 28, 2009 As Daniel0 pointed out, it's not a good idea. I was kind of thinking of saying this in my main post, but I wasn't sure how to articulate it correctly. A lot of PHP people used to (and still do >.>) store a bunch of settings in a "config" file for central access. They were doing it because it was easy, and fit in with a procedural way of doing things (as many PHP programmers learned PHP procedurally). I would think with the "more correct" way of doing things, in OOP, there would be less constants, etc... or at least not a lot of them, and none really being depended upon outside that file (class usually). Quote Link to comment https://forums.phpfreaks.com/topic/167732-solved-storing-lots-of-constants-outside-the-class/#findComment-884665 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.