Composer, a package manager for PHP

Composer is probably a bad name for a package manager in the sense where it is hard to research online by searching on the keyword ‘composer’ without receiving Beethoven as part of results. The composer I am referring to is this. Composer is a package manager tracking local dependencies of your projects and libraries.

Composer lets you specify your dependencies in a composer.json file looks like this:

[code lang=”javascript”]
{
“require”: {
“doctrine/common”: “2.1.*”,
“monolog/monolog”: “>=1.0,<1.2-dev”,
“swiftmailer/swiftmailer”: “>=4.1.2,<4.2-dev”,
“twig/twig”: “>=1.1,<2.0-dev”
}
}
[/code]

This file will install the components into the a vendor folder on the directory where you execute the following:

[code lang=”bash”]php composer.phar install[/code]

How to get json_decode or Services_JSON to return associative arrays

json_decode takes a JSON (JavaScript Object Notation) encoded string and converts it into a PHP variable. (More information on PHP documentation.)

[code lang=”php”][/code]

The above code would result in the following:

[code lang=”php”]object(stdClass)#1 (5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}

array(5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}
[/code]

When TRUE, returned objects will be converted into associative arrays. PHP’s associative arrays are amongst the easiest to use so i generally prefer an array to be returned.

When working on older PHP configurations such as in PHP 5.1, json_decode is not available. I use Michal Migurski’s Service_JSON. You can get the source code here.

The following does a json decode using the Services_JSON class.

[code lang=”php”]decode($json_text);
?>[/code]

If you use Services_JSON() instead, you are returned with StdClass. Using new Services_JSON (SERVICES_JSON_LOOSE_TYPE) returns you an array instead.

jQuery’s getJSON failing randomly in Internet Explorer

getJSON is perhaps the easiest method to erm get JSON with JavaScript using jQuery. JSON stands for JavaScript Object Notation, more information at Wikipedia.

The following piece of code is a really simple way to implement AJAX using the jQuery library:

I have a setInterval and each time Firefox will get the updated chat JSON every 10 seconds. But Internet Explorer has some issues, often return with old data.

And this is because of cache! The following code re-implements the above code to avoid caching issues.

To get back your data as JSON, remember to set dataType as “json”.

What cache: false does is that it appends a string of numbers such as “_=1211276828515” at the end of the URL.

GET http://localhost/chat.php?type=json&_=1211276828515

This number increases, it might be string of time in milliseconds or something, but it makes the browser thinks that the request is different from the previous.

I sat in front of the computer for an hour trying to figure out what’s wrong with my JavaScript or PHP code. Defeated by cache.