Hello. I have downloaded some ready projects and i saw that they seperate html from php. They are echoing everything through a class.For example they make a class for <html> tag for <body> etc.
This is driving me insane.Whats wrong with just having some classes and then mix html and php to display the data ?I think its lot simpler. Otherwise i get lost ..If they need 100 includes just to display a simple page.I read many books and everywhere i saw mixed mode.
So whats right? How should i design my sites? Represanting each html tag with a class how the heck then a designer could design something? Isnt simpler just have a template ?
Please help..

It is up to you. If you start building a large website with a lot of repeating code like headers, navigation menus, etc, you will realize how helpful the includes are.
Later on, you will realize that a database-driven site, with a lot of if/else statements becomes really hard to maintain, because you have a lot of code to verify and navigate within, so you will be looking in the direction of objects, where you call an object instance and it displays as needed. Changes become much more easier and the whole site becomes easily maintainable.

Books are good to give you the basics, they show how things may be made, but books not always show the best practices. It is good to read the books, as they would eventually explain a lot of the ideas implemented in the ready-made projects. A ready-made project is hard to understand, because actually you are going into reverse engineering, rather than development. It is good to understand the ideas behind ready-made projects and reduce their capabilities to the limits of your requirements.

Personally, I create a main template, and then divide repeating or interchangeable elements up into smaller templates.

I then combine the templates using a template class I've written, substituting templates in for placeholders, etc, before outputting the complete page.

I find it works really well. Essentially its following the MVC principle.


Where I work, we have designers that don't know php and seperating html and php will make the integration of the php code and the actually design a lot faster. This is very important when the website is handled by more than one person. If its just you, than its personal preference.

Yes i understand but how could a designer do something if they have a class for html tag and for each tag like <body> <head> etc..
IS this necessary ?
Also do you know any good books for oop in php ? I want to see some examples what they do..
Also is it a good practise to create a database object and then pass it through a function? Example
Users::registerUser(blah,database); where database is the active connection at the database.

I think having a class for each and every HTML element is serious overkill... although without seeing a specific example, I may be wrong. I find using one PHP class to generate HTML pages works very well.

With regard to passing a database connection, in my work, I have always used a database class, and each one of my scripts creates a new instance of the class, which will then retrieve an active DB connection. If your class has an object handle on a database class instance, you can reference it from within the class using $this->

Finally, tutorial wise, I've not used them. I had the advantage of learning on the job. Best option may be to Google for OOP PHP. There must be hundreds of tutorials on the net.


Thank you for your answer. I amnt sure if i understood well what you do about the database.
Suppose i have a page that uses database to get some news and to get some info about what the user has bought from the database again.
So News class and User class how should reference to the database?
creating a database like that
database = new Database();
News->showBlabla(database) ;
Or News and Usser should create its own database object so 2 connections per request ?

This article has been dead for over six months. Start a new discussion instead.