PHP: Call to undefined method on tr_TR locale

It’s one of weirdest errors in PHP - if not the weirdest - I’ve ever seen. If you set your locale to tr_TR, suddenly some classes or methods names are not available anymore. Really.

Following code:

<?php
setlocale(LC_ALL, 'tr_TR');
class SomeClass {}
$r = new ReflectionClass('SomeClass');
$r->newInstanceArgs(array());

results in Fatal error: Call to undefined method ReflectionClass::newInstanceArgs() error.

After quick research it turned out that it’s actually known bug, yet still not fixed since 2002! See #18556. In short, PHP simply uses internal tolower() function, which is locale-aware, but in Turkish there are some specific rules for I (undotted ı is the lowercase of I, and the dotted İ is the uppercase of i). So if you have class name of method name having upper I (newInstanceArgs, Image, etc), you’re affected.

The only workaround for now seems to be setting LC_CTYPE to some working locale, eg. setlocale(LC_ALL, 'tr_TR'); setlocale(LC_CTYPE, 'en_US');.

  1. by sobstel • April 2012