开发者

help with my static class logic with pushing new values into an array

I am trying to make a class that will load my .ini files for me. The rundown is that I specify the name of the application I'm working with, then give it a single or array of files for it to load for me. Once that is done I just call it like so.

Ini::path(BASE_PATH . "config" . DS);
Ini::set_app("testApp");
Ini::load(array("db", "web", "themer"));

then I'm trying to use it like this

echo Ini::$get['testApp']['someKey']['someValue'];

at the moment I can use it without the Ini::$app being set because I'm trying to add that features. i.e Ini::$get['someKey']['someValue'];

My problem is that I cannot figure out how to push more .ini files into the Ini::$get array. The usage should be something like this

Ini::push("anotherIni");

that is suppose to call my push开发者_C百科 method and push the new file into the array. But I can't seem to build the array correctly. Either that, or it is not possible to push a new value into a static array.

My array with the array_push attempt looks like this ounce outputted with a print_r()

Array ( [testApp] => )

otherwise it outputs this without me trying to push the new ini into the array

Array ( [testApp] => Array ( [db] => [web] => [themer] => ) )

Here is my Class file (Updated to reflect answers)

    class Ini {

    public static $get = array();
    public static $tmp = array();
    public static $path;
    public static $app;
    public static $push;

    public static function load($file)
    {
        if (is_array($file)) {

            foreach ($file as $ini)
                self::$get[self::$app][$ini] = parse_ini_file(self::$path . $ini . ".ini", true);

            //array_push(self::$get[self::$app], "testIni");
        } else {

            self::$get[self::$app][$file] = parse_ini_file(self::$path . $file . ".ini", true);
        }
    }

    public static function push($file)
    {
        self::$tmp[$file] = parse_ini_file(self::$path . $file . ".ini", true);
        array_push(self::$get[self::$app], self::$tmp[$file]);
    }

    public static function set_app($name)
    {
        self::$app = $name;
        self::$get[self::$app] = array();
    }

    public static function path($path)
    {
        self::$path = $path;
        return self::$path;
    }

    // TODO: Create 'write', 'delete', 'append' function to change ini values
}


The problem is in this line:

array_push(self::$get[self::$app], $file);

array_push() expects an array as it's first argument. I copy-pasted your class in to a PHP file, and then used the following code:

Ini::$app = 'Some App';

Ini::push('something.ini');

and receive the "array_push() expects..." error. This is because self::$get[self::$app] is a string, not an array.

So, you need a method that sets the first entry of the $get array to have a key of self::$app and a value of a blank array. This addtional method fixed it for me:

public static function set_app_name($name)
{
  self::$app = $name;
  self::$get[self::$app] = array();
}

Then use

Ini::set_app_name('Some App');


I'll answer your question, but IMHO there are several major concerns with your code:

  • Why is everything static? It would make more sense if the ini config was in an object. And if you just want to force its uniqueness, read about the singleton pattern.
  • The purpose of your $app static property is obscure. It needs to be initialized before loading or reading files. That's almost unreadable. At the very least, your code would be more clear with a method setDefaultApp($appName) instead of setting directly this attribute.
  • There are two syntaxes for adding an element $e at the end of an array $a:

    $a[] = $e;
    array_push($a, $e); // scarcely used
    

    With the latter syntax, $a has to be an array, it won't be initialized. So use the alternative syntax:

    self::$get[self::$app][] = $file;
    

    But I strongly recommend you opt for a more readable API. To your echo Ini::$get['testApp']['someKey']['someValue']; I'd even prefer echo $ini->get('testApp')['someKey']['someValue'];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜