1

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?

14
  • 1
    This seems to replace the ugliness of PHP's array syntax with the ugliness of a piece of code designed to get around it. Commented Jul 14, 2010 at 18:04
  • Indeed, but the ultimate goal is to simplify the writing of the config array, which can get really cluttered if written in PHP. This code will run a handful of times but will make my life (and whoever is going to maintain the rest of the code) easier. Commented Jul 14, 2010 at 18:07
  • You're using json_decode ... why not use json_encode as well? See php.net/JSON Commented Jul 14, 2010 at 18:07
  • 5
    If you're code is ever going to be maintained by someone else this is a bad idea. IMHO Commented Jul 14, 2010 at 18:09
  • 3
    As the author of this json_decode solution yesterday, I have to say that this is kind of insane. There's no way that this will be easier to maintain than the language-provided standard way of creating arrays that everybody already knows about because it's the first thing you learn in PHP 101. Arrays aren't that ugly, and you've now spent far more time creating a workaround to prevent having to write the word 'array()' a few times than you would've spent had you just bit the bullet and created the arrays by hand. Commented Jul 14, 2010 at 18:21

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

10 Comments

var_export lacks the $cleanKeys functionality (ie: generate array(1,2) instead of array(0 => 1, 1 => 2))
@Aircule Is that so important?
Not extremely, but makes things even more cluttered.
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.
@Aircule - Also, your code seems vulnerable to code injections(like SQL injections, but for PHP), since you don't escape your strings.
|
5

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.

Comments

2

var_export is your answer! Makes your a lot easier.

Comments

0

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.

1 Comment

You can use $s=var_export($x,true) them use your_parse_beuty($s) only to beautify spaces and lines.
0

Here is my solution for that

$data = json_decode(file_get_contents(__DIR__ . '/data.json'));
$code = var_export((array)$data, true);
$code = "<?php\n return " . preg_replace('/stdClass::__set_state/', '(object)', $code) . ';';
file_put_contents(__DIR__ . '/data.array.php', $code);

Data are taken from JSON file, but can be replaced with something else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.