I've been reading about locales in PHP and it seems setlocale()
has problems with threads. (I'm not too familiar with threads - the docs mention it is not thread safe)
I'd like to give my project the ability to deal with certain number formats and the Intl extension seems interesting.
http://php.net/manual/en/book.intl.php
Should I expect the same problems that setlocale()
has using the Intl extension?
The Intl extension is safe and very useful if you're working not working inside a framework.
For example, if you're using Symfony2, your programs will most likely crash when using Forms and Validators.
Ok, I was curious about this myself as well, so I devised a test.
First I tested
setlocale()
with these two files:and
Then I executed them in two seperate tabs. First
locale1.php
, that sleeps for 10 seconds after setting the locale, giving us time to executelocale2.php
in the meanwhile.To my surpriselocale2.php
isn't even allowed to change the locale correctly. It appearssleep( 10 )
inlocale1.php
hijacks the Apache/PHP process in such a way that it doesn't allowlocale2.php
to alter the locale in the meanwhile. It does however echo the date in the meanwhile of course, just not localized as you'd expect.Edit: sorry, scrap that. It appears
locale2.php
does change the locale andlocale1.php
then prints the English date in stead of Dutch after sleeping. So that does appear to be in accordance with what is expected behavior fromsetlocale()
. /EditThen, I tested
IntlDateFormatter
with these two files:and
and then executed them again in two separate tabs, the same way as with the first set of files. This does give the expected results: while
locale1.php
is sleepinglocale2.php
nicely prints a date in American-English according to American rules, after whichlocale1.php
nicely prints a date in Dutch according to Dutch rules.So, concluding, it appears
Intl
is safe from thatsetlocale
problem.But also mind Hyunmin Kim's answer of course. I couldn't comment on that, due to lack of experience with using
Intl
. I only recently discoveredIntl
.