-2

I'm creating an install page for a web project to define all the config variables and constants before it goes live. I think about keeping those configuration variables in a database but project manager wants them to be kept in a file. So there comes my problem. How do I define these variables, with const or define will work for this job? Example

    <?php
$email_parameter="example@stackoverflow.com"//we need to set this at install.php page and keep it that way
?>
Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
  • You need global variables for your project that are available for every page after app has been installed? I think you know the answer, global variables were and are bad, though I do not know your project specifics. Usually, config variables are kept in config files, that are accessed by Config class by parameter of which config variable you need. Constants should be what they are - constants, means they should not be config variables. – Siim Kallari Jan 22 '16 at 09:17
  • Also have a look at [this](http://stackoverflow.com/questions/2447791/define-vs-const) – Ali Jan 22 '16 at 09:21
  • @SiimKallari since project manager wants and presents the project that way, i will need to find a way to do that. i created a file for this and a class as well but what i want to know whis how to set those variables that needs to constant but also be able to change at the same time? First i thought screw it i will create file and write stuff in with fwrite but just seems wrong. – Atilla Arda Açıkgöz Jan 22 '16 at 09:24
  • That is not a constant when you need to change it in run time. Call it what it is: global variable and yeah, if your "Project manager" - lets call him with that title, because what he wants is as bad as it gets, wants it, then fwrite is your option. You could create Config class that gets instantiated every time after request and have that Class defined constants, then you can change them from Class instance. $global = new Config(), $global->email = new email. – Siim Kallari Jan 22 '16 at 09:29
  • i will try to convice her that is bad practice, i hope she understands what a horrible way she follows. and @Ali thanks for tips, that one was really helpful. – Atilla Arda Açıkgöz Jan 22 '16 at 09:36
  • I use it - maybe interesting? [just need a simple class to read configuration files of different types.](https://github.com/caseyamcl/Configula). All the configuration is kept in one file using `ini`, `json` or `xml`. I have different files for the different `runtime environment (dev, test, live)`. – Ryan Vincent Jan 22 '16 at 11:00
  • i checked all answers and comments and i appriciate you help guys. I did find a solution that satisfies my "project manager" and posted is as an answer here. i hope this help some other people as well – Atilla Arda Açıkgöz Jan 22 '16 at 14:28
  • @RyanVincent your first comment actuaaly got me that idea. Also thanks for that – Atilla Arda Açıkgöz Jan 22 '16 at 14:29

4 Answers4

1

You can use pattern Registry

<?php
/**
* Registry
*/
class Product
{

    /**
    * @var mixed[]
    */
    protected static $data = array();


    /**
     * add value to registry
     *
     * @param string $key
     * @param mixed $value
     * @return void
     */
     public static function set($key, $value)
     {
         self::$data[$key] = $value;
     } 

     /**
      * get value from registry
      *
      * @param string $key
      * @return mixed
      */
      public static function get($key)
      {
         return isset(self::$data[$key]) ? self::$data[$key] : null;
      }

      /**
       * remove value from registry
       *
       * @param string $key
       * @return void
       */
       final public static function removeProduct($key)
       {
           if (array_key_exists($key, self::$data)) {
               unset(self::$data[$key]);
       }
   }
}

/*
 * =====================================
 *           USING OF REGISTRY
 * =====================================
 */

 Product::set('name', 'First product');

 print_r(Product::get('name'));
 // First product
Artem Chernov
  • 856
  • 2
  • 8
  • 26
0

What you need is define

define('__CONSTANT_NAME__', 'example@stackoverflow.com');
Sha7x
  • 478
  • 3
  • 11
0

define, define('const_name','const_value')

  • are everywhere globally available (!),
  • are more flexible than const (at least with older PHP versions).

const

  • Are (at least in older PHP versions) available only in class definitions.

But

  • You cannot define constructs like arrays, objects, etc. (unless you are already using PHP Version 7.
  • define's do not honor namespaces (although you could differentiate yourself like this: "ns1/ns2/constant_name"

So, I suggest having your install script writing a config file e.g. config.inc containing define statements for the gathered values. Then let the runtime script(s) include (execute) it: require_once().

If (maybe later) constructs like arrays should be initialised the install script could add these statements too … and much more (even defining classes).

Executing, instead of reading, a config file:

  • Main advantage: can contain any executable statements, such as defining and initialising arrays. So, potentially any (even complex) setup constructs are available if needed
  • Main disadvantage: is more error prone, since it could fail running when changes introduce syntax errors, and it uses potentially more system resources

At the end I never could measure a big difference between executing and reading config files. And - since the config file is written by an install script, and not modified manually, the aspect of inserting syntax errors is not really important.

hherger
  • 1,660
  • 1
  • 10
  • 13
0

Well i checked all your answers and i thank you for that, helped me find the right path for me(at least one that satisfies my "project manager".).I ended up with this solution below:(Those values are for example)

   $f=fopen("config.php","w");
   $database_inf="<?php
     define('DATABASE_HOST', '".$database_host."');
     define('DATABASE_NAME', '".$database_name."');
     define('DATABASE_USERNAME', '".$database_username."');
     define('DATABASE_PASSWORD', '".$database_password."');
     define('ADMIN_NAME', '".$admin_name."');
     define('ADMIN_PASSWORD', '".$admin_password."');
     ?>";
  if (fwrite($f,$database_inf)>0){
   fclose($f);
  }

That is just and example to show how i figured it out. First i created and install php and set default url routing for that page when there is no config file setup. Then i asked the user input realted values and keep them in variables. Finally i created an config file and wirte them in. I hope this can help some oother people who deals similar situation.