简体   繁体   中英

PHP replacing CSS styling?

I am a casual web developer so usually I am not familiar with the details and deep design of CSS styles. As most of what I do require PHP finally all my pages execute some PHP.

I am finding myself feeling more comfortable implementing the design details in PHP included classes rather than in CSS.

For example rather than CSSing a header common across a site, I just include("header.php") on each page, "header.php" containing among others a class Palette with all the style definitions and stating the style directly in the elements (I have a personal allergy to having too many files)

So far I have found no inconveniences to this approach, in fact to me is more clear and flexible implementing the design in a procedural language than in declarative (I am a C++ programmer).

I am wondering if in the future I may regret this decision, what is wrong with this approach?

---- EDIT ----

Let me show a simple example to clarify what I mean (the example is not elaborated, definitely it would be better designed in a production case)

class Palette{

    var $colorBackground="#fff";
    var $colorText="#000";
}

//-------------------------------------
function styleInHeader($palette){

$css=<<<THECSS
    .header{
        color:{$palette->colorText};
        background-color:{$palette->colorBackground};
    }
THECSS;

return $css;
}

// when generating the <head>

$pal=new Palette();
echo style_general($pal);
  • The advantages: The definition in class Palette is unique across all the web site
  • I can apply some logic to the object palette, for example varying the colors it contains under some logic or even user interaction.
  • I can subclass Palette and passing instances of these subclasses would have flexibility, in fact I'd have all the advatages of proceduran/obejct oriented tech for styling.

It can work, if you're a good programmer, are very meticulous about applying this properly, and know what and why you're doing this. Ultimately, CSS solves things roughly the same way as code does, by setting up labels and loading data for them from a common source.

However, there is one very important reason to reconsider your approach:

You will never be able to get anyone else to work on this project, because they'll have no idea what you're doing.

But as long as you accept that this is a one-person-project through and through, I foresee no serious problems.

However, if you're doing this with the intention of making something big and professional, which might require other people to help you at some point in the future (or worse, getting paid to do this by someone), then I would really switch to actually using CSS for the project.

This is only the way I see if buy having to stile every single element through php is not only hard work but unnecessary too, and it will make your code "x"-times longer and harder to read, what If you won't open that php file for one month and then you decide to change something in it? You will not remember every parameter and you will forget to style some of them, and it will become a never ending loop of styling and uploading...

Also, why have to write the same style 100 times for 100 elements when you can target those from css all at once by adding a class?

The short answer: Using a CSS stylesheet is easier, flexible and makes your code a lot more readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM