Usage of server-side programming languages for websites
Tag: PHP
PHP 5.4.0 : Built-in web server
PHP: self:: vs. static::
I am seeing more and more PHP code with static:: key used for method calls instead of self::. Â Today I’ve finally found some time to examine the novelty. Â The page that is useful for more details is PHP’s late static binding. Â This functionality is available since PHP 5.3.0 so you might as well start using it.
I am a bit protective of my code, so self:: feels like a safer, more natural option. Â But after thinking about it for a bit, and discussing with my colleagues, I came to the conclusion that I should be using static:: instead of self::. Â It provides cleaner inheritance and minimizes code copy-pasting.
Non-alphanumeric backdoors
Beautifying PHP’s json_encode() output
I’ve been working a bit more with PHP and JSON recently and one of the things that annoyed me quite a bit was the single line output of the json_encode() function. Â Here is an example:
<?php $data = array( 'foo' => 'bar', 'bar' => 'qux', 'blah' => 'blah', ); echo json_encode($data); ?>
Poorly readable result (imagine having larger, more complex data structures like nested arrays):
{"foo":"bar","bar":"qux","blah":"blah"}
Apparently, since PHP 5.4.0 it became much easier to optionally format the output with some indentation. Â json_encode() function has an $options parameter, which was given an extra option – JSON_PRETTY_PRINT. Â Here is an updated example:
<?php $data = array( 'foo' => 'bar', 'bar' => 'qux', 'blah' => 'blah', ); echo json_encode($data, JSON_PRETTY_PRINT); ?>
And an updated output:
{ "foo": "bar", "bar": "qux", "blah": "blah" }
Very handy for debugging and for those bits of JSON that should be editable by hand.