In demand

ad1

Thursday, April 23, 2015

Unnecessary Evils: Configuration files PHP vs. Yaml vs. PHP

While working on the configuration file routine for Content Connection Kit I had some decisions to make. Do I go with the trendy YAML or the tried an true PHP array. I first heard tale of YAML when I began doing ruby  a few years ago  not ruby on rails but  ruby the language. After a while I became accustomed to it. Later I came to hate it. Somehow the syntax interfered with my learning of Ruby.  I dropped Ruby and forgot about Yaml after an employment change.



So when I saw that PHP projects were suddenly picking up yaml I had a dislike for this from the very start. Not because of the syntax but because I feel that PHP does not need YAML. PHP arrays are typically written in this format.

PHP


$settings = array(
        'site' => array(
            'name' => array(
                "value" => "Content Connection Kit", 
                "description" => "site name"),
            'description' => array(
                "value" => "Web Architecture for web programmers", 
                "description" => " site purpose"),
            "frontpage" => array(
                "value" => "index.php?blog/blog_latest" ,
                "description" => " page to open when site is found"),
            "theme" => array(
                "value" =>"default", 
                "description" => "templates and visuals"),
            "404_page" => array(
                "value" => "cck/not_found", 
                "description" => " when a page doess not exist"),
            "message_timeout" => array(
                "value" => 5, 
                "description" => " flash messages stop appearing after this time"),
            "user_timeout" => array(
                "value" => 3600, 
                "description" => " user is logged out automatically")
        ),

Update: I forgot about the latest format added in version 5.4+ because it was a RFC that I hated. Just because another language has this does not mean all should. PHP should not.

$settings = [
    'name'      => '555-9999',
    'description'  => '555-1212',
    'value'        => '555-1337',
];

The configuration file above is in PHP native write format. While I don't personally find it so bad. It does expose the reader to some of keywords used in PHP code and the => operator looks pretty intimidating to the unsuspecting non-coder. There is also lot's of room for typo's that might cause fatal errors at run-time.

 YAML


The alternative being Yaml a "human friendly" syntax. But what I like to call it is a insult to designers. Programmers opting to say that designers and others are not smart enough to learn a programming language safely so they get the safety version of a file written in Yaml. Supposedly Yaml files cannot be parsed by the PHP interpreter and cause errors in application stack. they are also supposed to be easier to read than the underlying languages raw code version.
defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost
While most of YAML’s syntax is intuitive and easy to remember, there is one important rule to which you should pay attention. Indentation must be done with one or more spaces; tabs are not allowed. You can configure your IDE to insert spaces instead of tabs when you press tab key, which is a common configuration among software developers to make sure code is properly indented and displayed when it’s viewed in other editors.
One of PHP strengths one that has been slowly over written by a popular belief that PHP should become a fully object oriented language like Java. Is it's usefulness as a template language. This characteristic is what made PHP a popular language and heir to ASP Classic. Another characteristic is array read syntax (dictionary style). Many use the syntax for what it was designed for, reading. But the same syntax is very powerful when used for writes of arrays. Not used as often because of it's verbosity and being unfamiliar to self-taught PHP newcomers. Although it is a syntax that is more than suitable for congiguration files and overlooked by trained programmers coming out of schools where they are taught arrays should look like the one shown in the above text.

PHP AGAIN

$settings['name']['value'] = "Content Connection Kit";
$settings['name']['description'] = "site name";

$settings['description']['value'] = "Web Architecture for web programmers";
$settings['description']['description'] = "site purpose";

$settings['frontpage']['value'] = "index.php?blog/blog_latest";
$settings['frontpage']['description'] = "landing page";

$settings['theme']['value'] = "default";
$settings['theme']['description'] = "templates and visual appearance";

$settings['404_page']['value'] = "cck/not_found";
$settings['404_page']['description'] = "templates and visual appearance";

$settings['message_timeout']['value'] = "5";
$settings['message_timeout']['description'] = "flash messages stop appearing after this time";

$settings['user_timeout']['value'] = "3600";
$settings['user_timeout']['description'] = "user is logged out automatically";

This is the syntax and format I have chosen to use in the Content Connection Kit. I consider it the best use of native PHP and readability. So PHP has two native formats for structuring data one of which is faster and just as easy to read as Yaml without any of the pitfalls. Why do PHP projects insist upon using Yaml which is only a pretty face and serves no real purpose. Part Two: Writing to the file Later after I clean up this post I'll write about another unnecessary evil, TWIG and other template languages!

2 comments:

  1. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing..
    Regards,
    php training institute

    ReplyDelete
  2. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Regards,
    Web designing course in chennai|Web design training in chennai

    ReplyDelete

Print this!