"is the php intl extension thread safe? " Code Answer

5

ok, i was curious about this myself as well, so i devised a test.

first i tested setlocale() with these two files:

<?php
# locale1.php
error_reporting( e_all | e_strict );

date_default_timezone_set( 'europe/amsterdam' );
setlocale( lc_all, 'dutch_nld' ); // awkward windows locale string

sleep( 10 ); // let's sleep for a bit here

echo strftime( '%a, %b %d, %y %x %z', time() );

and

<?php
# locale2.php
error_reporting( e_all | e_strict );

date_default_timezone_set( 'america/los_angeles' );
setlocale( lc_all, 'english_usa' ); // awkward windows locale string

echo strftime( '%a, %b %d, %y %x %z', time() );

then i executed them in two seperate tabs. first locale1.php, that sleeps for 10 seconds after setting the locale, giving us time to execute locale2.php in the meanwhile.

to my surprise locale2.php isn't even allowed to change the locale correctly. it appears sleep( 10 ) in locale1.php hijacks the apache/php process in such a way that it doesn't allow locale2.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 and locale1.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 from setlocale(). /edit

then, i tested intldateformatter with these two files:

<?php
# locale1.php
error_reporting( e_all | e_strict );

$dateformatter = new intldateformatter(
    'nl_nl',
     intldateformatter::full,
     intldateformatter::full,
     'europe/amsterdam'
);

sleep( 10 ); // let's sleep for a bit here

echo $dateformatter->format( time() );

and

<?php
# locale2.php
error_reporting( e_all | e_strict );

$dateformatter = new intldateformatter(
    'en_us',
     intldateformatter::full,
     intldateformatter::full,
     'america/los_angeles'
);

echo $dateformatter->format( time() );

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 sleeping locale2.php nicely prints a date in american-english according to american rules, after which locale1.php nicely prints a date in dutch according to dutch rules.

so, concluding, it appears intl is safe from that setlocale 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 discovered intl.

By Luis Vargas on October 15 2022

Answers related to “is the php intl extension thread safe? ”

Only authorized users can answer the Search term. Please sign in first, or register a free account.