harrisonad wrote:What do you mean? I saw some classes for building HTML strings.
Good objects are cohesive that is to say they round up some properties and methods relating to a specific task. Good OOP design is about identifying different responsibilities and encapsulating these.
The conceptual starting point is the standard presentation / domain / data access layering. The presentation layer is the UI: it receives input, decides what to do with it, manipulates the domain and creates a browser page. In MVC the presentation layer is further split into controller and view but the really important separation is between presentation/domain. The domain is the all the business logic: things like calculating the interest due on an account, identifying user permissions, and so on. Domain objects add some kind of meaning to raw data. Data access is the code which gets raw data from persistent storage - a database, csv files, whatever.
That's the broad brush: each layer will break down further until you finally get to individual responsibilities - those will be your classes. Formatting (html) is firmly in the presentation layer. You can mix html and php but only if the code is carrying out presentation logic. Perhaps some conditionals such as:
Code: Select all
if($account->isOverdrawn()) {
echo <span style="color: red;">$account->getBalance()</span>;
} else {
echo $account->getBalance();
}
"Presentation logic" is a slightly confusing term: it simply means "formatting logic" rather than all aspects of the presentation layer.
Note that "is overdrawn" is a domain concept. A boolean flag would be set by a domain object. The view can decide how it wants to present this - perhaps it would be formatted differently on different pages or, if you wanted a CLI version, you wouldn't format with html at all. The fact that you've clearly separated domain and presentation allows you create different kinds of output without duplicating code. The domain remains the same.
I'm not a fan of classes which output tables and the like: I prefer just to define sets of data in the main body of the code and then do all the formatting (ie add the html) in the template.