#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    199
    Rep Power
    17

    User Editable Constants


    I have a file (well, I have several of them) that look something like this:
    PHP Code:
    class ABC_constants {
        
    // Each constant MUST be on one row. Do not split rows for readability!
        
    const abcSOMETHING " ... content ... "
        const 
    abcSOMETHINGELSE " ... content ... "
        const 
    abdWACKAWACKAWACKA 123


    And I want to build an interface to allow changing these constants. I propose to use some simple code to edit them like this:
    PHP Code:
    $id "abcSOMETHING";
    $new_line " abcSOMETHING = /"something./""$contents file_get_contents($dir);
    $new_contents"";
    if( 
    strpos($contents$id) !== false) { // if file contains ID
        
    $contents_array preg_split("/\\r\\n|\\r|\\n/"$contents);
        foreach (
    $contents_array as &$record) {    // for each line
            
    if (strpos($record$id) !== false) { // if we have found the correct line
                
    $new_contents .= $new_line// change record to new record
            
    }else{
                
    $new_contents .= $record "\r";
            }
        }
        
    file_put_contents($dir$new_contents); // save the records to the file
        
    echo json_encode("Successfully updated record!");

    Is there a better way?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,321
    Rep Power
    9645
    Don't store data in code. There are plenty of other alternatives that are much more appropriate.

    How about a database?
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,779
    Rep Power
    4536
    "user editable constants" ... you realize how that sounds, don't you?

    Why not use a config file and parse_ini_file()? Do you need a web interface to update config values? Once you have them in an array, it's easy enough to loop through to create a form and update the file.

    -John
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    199
    Rep Power
    17
    Well... These constants need to be editable - changeable. Some are things like tool-tips, and probably wouldn't change much at all. But others are specific numbers that are used in more than one place by more than one process, but can be changed (not often) depending on changes in policy, maybe it's 20 now, but we decide we really like 30. It could be "time-outs" and we discover we can tune the system by changing that number now and then. It could be how big the offset is in a recursive loop that's part of a guessing algorithm, and we like to tweak it now and then without having some programmer **** up some file that they really don't understand (although my commenting and documentation are impeccable) As well, it is possible that this application is used at several facilities where the individual details vary but the application processes are the same. There could be many reasons not to bring on board a programmer after I'm done and gone to hack the code and find everywhere I'm using this constant, and change it, especially if we want to change these things every few weeks or months. And, I *DON'T* want to hit the database *EVERY* time I load a class or script, hence "constant".

    Suppose I do save them in a table, but create a new file that may (or may not) periodically be created and overwrites the old file when ever there are changes to the numbers / data in the table?
    Last edited by Arty Zifferelli; May 20th, 2017 at 01:51 AM.
  8. #5
  9. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,321
    Rep Power
    9645
    A quick question:
    Originally Posted by Arty Zifferelli
    And, I *DON'T* want to hit the database *EVERY* time I load a class or script, hence "constant".
    Are your pages not hitting the database already?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    199
    Rep Power
    17
    Are your pages not hitting the database already?
    Of course. But if I can shave so much as a fraction of a microsecond by reading / including (and possibly caching?) a static file of values verses running a query and loading the values, I'll do it, why not?

    I want to spend the time reading and writing the application results, not the configuration variables.

    I could be thinking about it all wrong... I mean, I don't want dopey constructions just because in my mind it's more "elegant"...
    Last edited by Arty Zifferelli; May 20th, 2017 at 03:24 AM.
  12. #7
  13. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,079
    Rep Power
    4101
    As mentioned earlier, don't try to figure out how to edit your PHP code dynamically, just use a configuration file and load it. I tend to use json files these days.

    Rather than pull the values from class constants have a configuration class that can read the values from your configuration file.

    Code:
    <?php
    
    class Configuration {
        private static $settings;
    
        private static function loadConfiguration(){
            if (!self::$settings){
                $content = file_get_contents('configuration.json');
                self::$settings = json_decode($content, true);
            }
        }
    
        public static function getValue($group, $key){
            self::loadConfiguration();
            if (isset(self::$settings[$group][$key])){
                return self::$settings[$group][$key];
            } else {
                return null;
            }
        }
    }
    
    Configuration::getValue('ABC', 'SOMETHING');
    Configuration::getValue('ABC', 'SOMETHINGELSE');
    Configuration::getValue('ABC', 'WACKAWACKAWACKA');
    Configuration.json:
    Code:
    {
        "ABC": {
            "SOMETHING": " ... content ... ",
            "SOMETHINGELSE": " ... content ... ",
            "WACKAWACKAWACKA": 123
        }
    }

    Comments on this post

    • MrFujin agrees
    • Catacaustic agrees
    Last edited by kicken; May 23rd, 2017 at 09:17 AM.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  14. #8
  15. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,779
    Rep Power
    4536
    Just use a config file, Arty.
    -- Cigars, whiskey and wild, wild women. --
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    199
    Rep Power
    17
    Just use a config file, Arty.
    Still got to edit it. Didn't really want to deal with PEAR.
  18. #10
  19. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,079
    Rep Power
    4101
    Originally Posted by Arty Zifferelli
    Still got to edit it. Didn't really want to deal with PEAR.
    Not sure what PEAR has to do with anything.

    Just create your UI to edit things however you want. Then just write out the json to a configuration file when done, easy-peasy.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    199
    Rep Power
    17
    Not sure what PEAR has to do with anything.
    PEAR has some functions to manage INI files.

IMN logo majestic logo threadwatch logo seochat tools logo