views:

48

answers:

4

I don't know if the title explains it very well, so here's what I'm trying to achieve.

With this script people can change the value of configuration variables on the fly like so:

Config::write('General.load', array('plugin1','plugin2'), true);

In that example, it changes the General.load config variable to an array, and sets the 3rd param to true, which means that it can be saved.

What I'm trying to incorporate is a method the save part, when a user calls Config::save() it loops through any config variables that were written to and which have had the 3rd parameter set to true.

It works perfect for booleans, strings, ints, floats etc. But when it comes to arrays, by default, it would just write "Array" to the file, instead of "array('plugin1','plugin2')".

So I went ahead and began developing a little script that converts arrays to a string based array.

Here is the function (I had to put it on pastebin, formatted strange here): http://pastebin.com/HsUG9n5D

It works, to a degree.

If you supply it with something like this:

$array_to_string = '';
$array = array(
    'welcome',
    'to',
    'my',
    array('website')
);
array_build($array_to_string, $array, 0);
die($array_to_string);

It will output:

array('welcome', 'to', 'my', array('website')),

It nearly worked. However if I input something like this:

$array_to_string = '';
$array = array(
    'welcome',
    'to',
    'my',
    array('website'),
    array(array('goodbye', 'foo' => 'bar'))
);
array_build($array_to_string, $array, 0);
die($array_to_string);

The output is:

array('welcome', 'to', 'my', array('website')array(array('goodbye', 'foo' => 'bar'), ))

I've gone over the code, and I'm not quite sure what I can do to fix it. Variable referencing is still a tad complicated for me.

If anyone has any ideas on what I've done wrong, let us know.

Cheers.

A: 

What you are looking for is serialize() and its counterpart unserialize()

Dennis Haarbrink
I've taken a look at serialize, but the thing is the end-user may want to change the variable by hand in the config file. Variables are written in the config file like so: `Config::write('Plugin.load', array('geshi/geshi'));` I want to be able to write to the config file in the same way.
Jason Lewis
serialize is the best way out of the box. If you want more customized output, you will need to write a function to convert the array into the sort of output you want.
Jani Hartikainen
@Jason Lewis: Then I guess `var_export()` or `json_encode()` are the best OOTB solutions for you, as stated above.
Dennis Haarbrink
+1  A: 

Try

  • var_export — Outputs or returns a parsable string representation of a variable

Example from PHP Manual:

$a = array (1, 2, array ("a", "b", "c"));
var_export($a);

// output

array (
  0 => 1,
  1 => 2,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)

See my answer to PHP Reading File on how to use this in a config class.

Gordon
var_export is good, but it's not as clean as I was hoping for. But if my function is really not going to be any good, I think I might have to settle for var_export.
Jason Lewis
+2  A: 
  • var_export() is good when you want to save it as a string that is actually valid PHP and can be parsed as such.

    This appears to most closely match your example.

  • serialize() and unserialize() are good for storing an array as a string internally to your PHP application.

  • json_encode() and json_decode() are good when it needs to be interoperable with other languages, or other versions/builds of PHP, or stored in persistent storage, etc for maximum portability of your data.

thomasrutter
A: 

You might want to try the Pear Config package.

Mike