PHP导入单个类,带有一些css和javascript文件

I'm having a few css and js files. So i want to have those in the single class to be imported to different php files.

class generalStyles {

function getGenStyles(){
    echo "<link rel='stylesheet' href='bootstrap/css/bootstrap.css'/>
        <link rel='stylesheet' href='bootstrap/css/bootstrap-theme.css'/>
        <link rel='stylesheet' href='css/general.css'/>

        <script type='text/javascript' src='js/jquery-2.0.3.min.js'></script>
        <script type='text/javascript' src='bootstrap/js/bootstrap.min.js'></script>
        <script type='text/javascript' src='js/jquery.cookie.js'></script>
        <script type='text/javascript' src='js/basket.js'></script>
        <link rel='shortcut icon' href='../favicon.ico' type='image/x-icon''>
        ";
}
}

it is OK when I do import class to the files that are on the same level from root, but when I'm trying to import this to other level - styles are not applied.

include("../../generalStyles.php");
    $styles = new generalStyles();
    $styles->getGenStyles();

How can I fix that? thanks!

You could either use absolute paths like Victor Häggqvist suggested or if that is not suitable for you you can make the path a parameter of your function:

function getGenStyles($path){
    echo "<link rel='stylesheet' href='" . $path . "bootstrap/css/bootstrap.css'/>
        <link rel='stylesheet' href='" . $path . "bootstrap/css/bootstrap-theme.css'/>
        <link rel='stylesheet' href='" . $path . "css/general.css'/>
        ....

and then call it like this for example, depending on the location of the actual php file where you make the include.

$styles->getGenStyles('../');

It is your css and JS imports that assumes that you are in one place.

To make it work every where you could specify the entire domain path to the files.

Example:

<link rel='stylesheet' href='http://example.com/css/general.css'/>