1

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.
5
  • I would say that this is quite opinion-based.. Are you actually styling each single element through php? What happens when you need to change a css parameter for a list of elements? Commented Dec 5, 2014 at 10:25
  • What if you redesign your application? Having to modify only css files certainly seems to be easier than having to dig into the PHP code. Commented Dec 5, 2014 at 10:27
  • 4
    Most people have an allergy to 2000 lines of code in 1 file. Commented Dec 5, 2014 at 10:29
  • Now generate CSS has become a top sport theft, "it works" became "it has to work well." Performance, maintainability, scalability, extensibility, cleanliness, modularity, ability to function responsive, etc. are no longer idealistic concepts, are now tangible goals and even pre-requisite. That is why, as far as possible, be avoided inline and internal style sheet ! Commented Dec 5, 2014 at 10:40
  • I edited the post to show an example. I think it replies the concerns expresed here Commented Dec 5, 2014 at 12:41

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Agree totally... well except the "never", the PHP code could be well designed enough to make autoevident what's supposed to do
No matter how well designed you're making this, at BEST you're asking people to learn a completely new framework of a sort they've never seen before. More likely, you're looking at the equivalent of learning a completely new way of programming websites.
0

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.

2 Comments

I didn't meant styling each element with php, maybe I was not clear in my first post, check the edit of the original post with a sample of the idea.
Well ok, now the only thing that remains is that only you can work on it, it will not be that easy to read by someone else but I guess this works fine for you if it's a one man project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.