Jump to content

[SOLVED] Storing lots of constants outside the class?


ifubad

Recommended Posts

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,

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.