Search code examples
phpwordpressphp-5.3php-5.5

Modifying new code to support PHP5.3


I'm using Roots.io wordpress starter theme. It's coded for PHP 5.5 but the server I'm posting the site on is running PHP 5.3

I need to change this code to be supported by the PHP on the server, but don't know how.

function asset_path($filename) {
    $dist_path = get_template_directory_uri() . DIST_DIR;
    $directory = dirname($filename) . '/';
    $file = basename($filename);
    static $manifest;

if (empty($manifest)) {
    $manifest_path = get_template_directory() . DIST_DIR . 'assets.json';
    $manifest = new JsonManifest($manifest_path);
}

if (WP_ENV !== 'development' && array_key_exists($file, $manifest->get())) {
    return $dist_path . $directory . $manifest->get()[$file];
} else {
    return $dist_path . $directory . $file;
}
}

The issue is in this line:

return $dist_path . $directory . $manifest->get()[$file];

The [$file] is confusing PHP I think but no idea how to modify this. Any tips? If more code is needed please let me know.


Solution

  • You'll need to split that return as requesting an index from a method call I think started getting supported in 5.4.

    Try splitting it out.

    $val = $manifest->get();
    return $dist_path . $directory . $val[$file];
    

    For reference this is know as array dereferencing. You can find more information about it here.