59

I cannot find any examples, in books or on the web, describing how one would properly initialize an associative array by name only (with empty values) - unless, of course, this IS the proper way(?)

It just feels as though there is another more efficient way to do this:

config.php

class config {
    public static $database = array (
        'dbdriver' => '',
        'dbhost' => '',
        'dbname' => '',
        'dbuser' => '',
        'dbpass' => ''
    );
}

// Is this the right way to initialize an Associative Array with blank values?
// I know it works fine, but it just seems ... longer than necessary.

index.php

require config.php

config::$database['dbdriver'] = 'mysql';
config::$database['dbhost'] = 'localhost';
config::$database['dbname'] = 'test_database';
config::$database['dbuser'] = 'testing';
config::$database['dbpass'] = 'P@$$w0rd';

// This code is irrelevant, only to show that the above array NEEDS to have Key
// names, but Values that will be filled in by a user via a form, or whatever.

Any recommendations, suggestions, or direction would be appreciated. Thanks.

AllisonC
  • 2,973
  • 4
  • 29
  • 46
NYCBilly
  • 870
  • 1
  • 8
  • 11

2 Answers2

64

What you have is the most clear option.

But you could shorten it using array_fill_keys, like this:

$database = array_fill_keys(
  array('dbdriver', 'dbhost', 'dbname', 'dbuser', 'dbpass'), '');

But if the user has to fill the values anyway, you can just leave the array empty, and just provide the example code in index.php. The keys will automatically be added when you assign a value.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • You have to do this outside of the class, though, since you can't call any functions in class variable declarations. That *may* result in more code, or initialization code appearing in places where you don't expect to see it. – BoltClock Oct 14 '12 at 07:14
  • @BoltClock Yeah, I wouldn't choose this option anyway. Those extra characters you need for 'normal' array initialization make it much clearer to me what the code does. I would leave it as it is. Just showing that if you want to, there are ways to do it. :) You could do this in a constructor, but not for a static class, of course. – GolezTrol Oct 14 '12 at 07:19
  • Again, static, was also just an example scenario... But what I really was looking for was the shortened method to filling them. It seems that, although I got my answer, that the original is the best way to go - assuming that from the comments above. Thanks guys... – NYCBilly Oct 14 '12 at 07:24
  • didn't know about array_fill_keys and although it has it's limits this is a very nice & efficient approach. – But those new buttons though.. Sep 12 '14 at 18:43
3

First file:

class config {
    public static $database = array();
}

Other file:

config::$database = array(
    'driver' => 'mysql',
    'dbhost' => 'localhost',
    'dbname' => 'test_database',
    'dbuser' => 'testing',
    'dbpass' => 'P@$$w0rd'
);
Seth
  • 1,353
  • 1
  • 8
  • 17