Search code examples
phpphp-5.2

self:: in PHP5.2


How to make this backwards compatible with PHP5.2? It works on 5.3 and later

error

Fatal error: Cannot call method self::utf8_dec() or method does not exist

code

private function utf8_decode($arr){
    array_walk_recursive($arr, 'self::utf8_dec'); // <----- error

    return $arr;
}

private function utf8_dec(&$value, $key){
    $value = utf8_decode($value);
}

Solution

  • Instead of self, you can use the name of the class directly. It's not as flexible but it should work.

    static private function utf8_decode($arr){
        array_walk_recursive($arr, 'YourClass::utf8_dec');
    
        return $arr;
    }
    
    static private function utf8_dec(&$value, $key){
        $value = utf8_decode($value);
    }
    

    Also you need to prefix the methods with the static keyword.