My previous post was not very good. Is should at least be:
Code: Select all
class Response {
var $_headers = array();
var $_redirect = '';
var $_content = '';
function setRedirect($location) {
$this->_redirect = "Location: $location";
}
function addHeader($string, $replace=true) {
$this->_headers[$string] = $replace;
}
function addContent($content) {
$this->_content .= $content;
}
function out() {
if ($this->_redirect) {
header($this->_redirect);
exit;
} else {
foreach ($this->_headers as $string => $replace) {
header($string, $replace);
}
echo $this->_content;
}
}
}
$response = new Response();
$response->addContent('Hello World!');
$response->setRedirect('other_page.php');
$response->out();
The Ninja Space Goat wrote:Somebody challenged this solution, saying it is basically the same as the output buffering functions. My question is... what is the difference between doing what arborint does and standard ob_start() ob_flush() etc.?
For me, this basic class above does not do anything functionally any better than output buffering. It is more testable though. I don't know which would be better as far as memory and speed.
For HTML page building the obvious improvements would be to add methods like these:
Code: Select all
function addMetaTag() {}
function addScript() {}
function addStyle() {}
function addStylesheet() {}
function addHead() {}
function addBodyParam() {}
The alternative to this is to make the response class look a little like a Template class with set() and render() methods, but with a setRenderer() method to all a template object to provide the content instead. Then you can pretty create hierarchical render trees (composite/component pattern) that can mix and match different template types for different parts of the page. This is what I am currently doing.
The Ninja Space Goat wrote:(Sorry to put you in the hotseat arborint, but I am very interested)
Please go wild. I have found that when my code gets ripped to shreds -- I learn the most!
