views:

111

answers:

4

Following my last post about better syntax for arrays, I've decided to use JSON format for arrays and use a method to convert them into PHP code.

Note that the ultimate goal of this is to be write a $config array in JSON and translate that into PHP code (so I can avoid having to use PHP's ugly array syntax):

The function works fine for arrays of arbitrary size and dimension, maybe I could improve it by having it automatically indent, but there isn't much more. Does anyone here any suggestions on how to make it better?

+3  A: 

Have you seen var_export? It looks like you've reinvented it.

Also, if you're defining your config in JSON, why are you turning it into PHP syntax? Why not just read it in as JSON, json_decode it, and then use it as is? It seems like maintaining the data serialized in both PHP format, and JSON format is really ugly and unnecessary.

I would also echo what amber said in the comments... it seems like you've replaced the somewhat ugly, but very straightforward PHP array syntax with a much uglier hack. No offense, but this doesn't seem like a very good idea. Here's an example of a config file from the Kohana PHP framework. I don't find this file to be particularly ugly to read, and it's native PHP, so any PHP developer can work with it.

notJim
var_export lacks the $cleanKeys functionality (ie: generate `array(1,2)` instead of `array(0 => 1, 1 => 2)`)
quantumSoup
@Aircule Is that so important?
luiscubal
Not extremely, but makes things even more cluttered.
quantumSoup
Aircule, those things are actually identical. You should format your array like the example I posted, so that it's readable. This would make it feel less cluttered.
notJim
@Aircule - Also, your code seems vulnerable to code injections(like SQL injections, but for PHP), since you don't escape your strings.
luiscubal
@luiscubal Irrelevant, since only me (or another developer, if any) will be using this.
quantumSoup
@notJim The problem with formatting that is that each of these files is going to be huge, with several levels of indentation. None of the PHP beautifiers do the trick. I guess I should code that, huh?
quantumSoup
@Aircule Not irrelevant, because one day you'll forget, and use this function with some data - ANY data - that's user-provided(and, as a result, untrusted). Then you'll regret it. Also, it'll be a huge inconvenience later since your code won't work properly if strings include the ' character and you spend ten hours trying to figure out why. Also, if you get used to code like this, you might get used to write unsafe code, even for projects that interact with real(aka evil) users, which might be a problem.
luiscubal
@luiscubal Sanitizing user input is something I definitely won't forget. And letting users directly inject PHP code is something I will never do. About the ' character; nice catch, but the PHP error would (hopefully) be verbose enough for me to catch that.
quantumSoup
@Aircule - Still, if you're really going with this function instead of JSON or var_export, then I'd solve the ' problem sooner rather than later.
luiscubal
@luiscubal Already did it :)
quantumSoup
A: 

var_export is your answer! Makes your a lot easier.

Dennis Haarbrink
+4  A: 

I will make one last effort to convince you not to do this. You asked for ways to improve upon your idea, and the best improvement you could make would be to not do it.

Here's is the PHP version of a config file from Kohana:

$test = array(
    'default' => array(
        'type'       => 'mysql',
        'connection' => array(
            'hostname'   => 'localhost',
            'database'   => 'kohana',
            'username'   => FALSE,
            'password'   => FALSE,
            'persistent' => FALSE,
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => FALSE,
        'profiling'    => TRUE,
    ),
    'alternate' => array(
        'type'       => 'pdo',
        'connection' => array(
            'dsn'        => 'mysql:host=localhost;dbname=kohana',
            'username'   => 'root',
            'password'   => 'r00tdb',
            'persistent' => FALSE,
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => FALSE,
        'profiling'    => TRUE,
    ),
);

And here is the JSON version:

var test = {
    "default": {
        "type": "mysql",
        "connection": {
            "hostname": "localhost",
            "database": "kohana",
            "username": false,
            "password": false,
            "persistent": false
        },
        "table_prefix": "",
        "charset": "utf8",
        "caching": false,
        "profiling": true
    },
    "alternate": {
        "type": "pdo",
        "connection": {
            "dsn": "mysql:host=localhost;dbname=kohana",
            "username": "root",
            "password": "r00tdb",
            "persistent": false
        },
        "table_prefix": "",
        "charset": "utf8",
        "caching": false,
        "profiling": true
    }
};

They're nearly identical. I really fail to see what you're gaining.

notJim
A: 

OK, after hearing all the feedback from everybody here, I've decided to make a "compromise." My main beef with the existing array syntax is its bad readability, which can certainly be improved (a lot) by using indentation.

Because I am lazy to indent (and the files I am writing are huge), I opted for JSON (or any syntax that's more readable than PHP's). I didn't make myself very clear, but another strong reason why I am using the JSON format because many other people will be looking at these config files. Most of them are not PHP-savvy and JSON is a much more human-readable format.

Unfortunately PHP code formatters/beautifiers out there don't do anything to array formatting, so I coded my own. It is based on that ugly piece of code I wrote above (and it is uglier), but it does the job.

The result is now I basically have an array beautifier, and I can generate readable native PHP code while being lazy. That's all I wanted, thanks everybody for the suggestions and pointers.

PS: Here's the beautified Kohana config array I generated with my function:

array (
    'default'   => array (
        'type'         => 'mysql',
        'connection'   => array (
            'hostname'   => 'localhost',
            'database'   => 'kohana',
            'username'   => false,
            'password'   => false,
            'persistent' => false
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => false,
        'profiling'    => true
    ),
    'alternate' => array (
        'type'         => 'pdo',
        'connection'   => array (
            'dsn'        => 'mysql:host=localhost;dbname=kohana',
            'username'   => 'root',
            'password'   => 'r00tdb',
            'persistent' => false
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => false,
        'profiling'    => true
    )
);

Which doesn't look bad at all.

quantumSoup