Getting filepath of a specific asset I wanted to make my assets semi-dynamically so I made this utility class for it:

Assets PHP class:


&lt;?php

    class Assets {

        private static $paths = array(
            'CSS' =&gt; '/assets/css/',
            'JS' =&gt; '/assets/js/',
            'FONTS' =&gt; '/assets/fonts/',
            'IMAGES' =&gt; '/assets/images/'
        );

        public static function get($file) {
            $fileType = pathinfo($file, PATHINFO_EXTENSION);

            switch (strtolower($fileType)) {
                case "css":
                    return self::$paths['CSS'] . $file;
                case "js":
                    return self::$paths['JS'] . $file;
                case "otf":
                case "eot":
                case "ttf":
                case "woff":
                case "woff2":
                case "svg":
                    return self::$paths['FONTS'] . $file;
                case "png":
                case "gif":
                case "jpg":
                    return self::$paths['IMAGES'] . $file;
                default:
                    throw new InvalidArgumentException("Filetype is not supported.");
            }
        }
    }
?&gt;


Usage:


&lt;link href="&lt;?php echo Assets::get('bootstrap.css') ?&gt;" rel="stylesheet"&gt;
&lt;script type="application/javascript" src="&lt;?php echo Assets::get('jquery.js') ?&gt;"&gt;&lt;/script&gt;


Is this efficient and how can I make it better, more dynamically and further.
Also I want to note I made this also to shorten my asset urls.
I think this class is largely fine as a display/view generation helper class, but worry about its broader utility.  Let me ask, if the PHP script in your usage example already owns the logic for the specific filename that it wants to include, shouldn't it also know the specific for the path to the file at this point already?  This HTML-rendering logic already knows know the most specific information about the asset that is to be included (the asset filename) but defers the more general information about the asset (its directory location) to the class.  That seems like an odd division of responsibility.  Additionally, your approach seems limited to only using asset files which are hosted on the same domain as the page being rendered, as I don't see how your solution supports full URL references.

Since you are going down the path of trying to dynamically inject what are basically config-driven values into this HTML template, I would suggest you truly decouple the configuration of included assets from the HTML template, perhaps striving more towards a solution like:

&lt;html&gt;
&lt;head&gt;
    &lt;!-- Other HTML HEAD contents --&gt;
&lt;?php
// You could obviously use templating library as well,
// but raw PHP "spaghetti code" shown here for demonstration
foreach(Assets::getCssIncludes() as $url) {
?&gt;
    &lt;link rel="stylesheet" href="&lt;?php echo $url; ?&gt;"&gt;
&lt;?php
}

foreach(Assets::getJsHeadIncludes() as $url) {
?&gt;
    &lt;script type="application/javascript" src="&lt;?php echo $url; ?&gt;"&gt;&lt;/script&gt;
&lt;?php
}
?&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;!-- BODY content --&gt;
&lt;?php
foreach(Assets::getJsEndOfBodyIncludes() as $url) {
?&gt;
    &lt;script type="application/javascript" src="&lt;?php echo $url; ?&gt;"&gt;&lt;/script&gt;
&lt;?php
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;


With this sort of approach you can totally decouple your template from needing to have any hard-coded references to it's asset dependencies.  You could derive these from configuration and expose via "Assets" (or similar class).

This is advantageous as you begin to want to deploy different configurations of a page into different environments (i.e. dev, staging, production).  You could have production configured to inject the current stable production assets, while pre-prod environments can injects other assets versions or new assets that are not suitable or not yet deployed to production.