为什么我们不应该在PHP类中包含html标记? [关闭]

At the end of this answer, RobertPitt wrote "you will learn as time goes on that you should not be using html directly within your class, and that you would split the above into several classes".

So why is this considered to be bad practice?

The part of the answer you've referenced appears to contradict itself, in that it states you shouldn't use HTML in your classes, but then suggests you create Header and Footer classes, which presumably would generate HTML.

The other answers at present do not appear to address your question either. They correctly point out that the answer you've referenced relates to separation of concerns but that's not really what you're asking, at least from your question title.

If you are in fact asking whether writing a class that's responsible for generating HTML markup is bad practice; then the short answer it is that isn't.

Popular frameworks sometimes include classes dedicated to this purpose, such as CHtml in Yii.

Because the class itselfs should be modular. An authentication class for example you may want to use in a lot of different projects with complete different HTML markup.

Try always to separate GUI and logic as much as you can so you get pieces of code you can easily reuse in other projects.

Mixing application logic, business logic and view parts (html in this example) inside a single place violates both the Separation of concerns and Single Responsibility design principles. So, it is really not a good practice.

Update after other answer and comments:

Generating html parts, strings or any kind of data in dedicated classes is ok. There are nothing wrong with this. You may produce/use/format or do whatever you want with html parts, strings in your classes or methods. Problem is when you decide to do this in another class or method (part) of your application which is main responsibility is handling something else. Writing code without any kind of principle or convention, leads to build unmaintainable, hard-to-understand, hard-to-read, tasteless spaghetti applications which nobody wants to learn/maintain/extend or use. This is not a good thing.