cowfish Posted May 6, 2008 Share Posted May 6, 2008 I want to make a Page class. The Page class has methods to add the header, body and footer. Obviously, I need to pass in the *entire* body into the page class. Usually, that body is pretty big. Is it a bad idea to assign such a enormous string to one variable? What I mean is this: class Page { // instance vars private $title; private $meta; private $whatever; public function addHeader($header) { //implementation } public function addBody($body) // $body variable is a massive string { //implementation } public function addFooter($footer) { //implementation } } There is also another example here: http://www.google.com.au/search?hl=en&q=PHP+Page+class+Anthalogy&btnG=Search&meta= Quote Link to comment Share on other sites More sharing options...
MattDunbar Posted May 7, 2008 Share Posted May 7, 2008 Shouldn't be an issue, every post in the forums is loaded into a huge string variable. 10 to 20 large strings can be loaded into a page if you open an active thread on these forums as well. As a general statement, it won't hurt. But, you might want to consider letting the body be built as an object itself, allowing you to add things such as links through functions allowing for no HTML knowledge. It could be done so many ways, its all up to you really. Quote Link to comment Share on other sites More sharing options...
btherl Posted May 12, 2008 Share Posted May 12, 2008 PHP won't crash if that's what you're worried about There may be performance hits if you do a lot of manipulation on the large string though, especially manipulation that modifies the start or middle of the string. That will typically require copying the whole string. If you plan to manipulate the body frequently, Matt's suggestion of an object may be more efficient. I have used php for strings around 500MB in length (that's 500 million characters, in one variable). Quote Link to comment Share on other sites More sharing options...
nloding Posted May 12, 2008 Share Posted May 12, 2008 I'm working on a page class myself and am doing a combination of the above. My drawHead (the HTML <head> section) has inside of it drawCSS, drawJS, drawMeta, drawTitle methods that it calls. Then I have a drawHeader, drawFooter, etc. Example: class Page { private $_page; private $_db; private $_err; public function __construct() { global $factory; $this->_db = $factory->getObject('dbPage'); $this->_err = $factory->getObject('error'); } public function setPage($title) { $this->_page = $title; } public function drawHead($special = null) { $head = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"; $head .= "\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"; $head .= "\n<head>"; $this->_db->query("SELECT title FROM pages WHERE keyword='$this->_page'"); $result = $this->_db->fetchAssoc(); $head .= "\n\t<title>" . SITENAME . " | " . $result['title']; $head .= $this->_drawHeadMeta(); $head .= $this->_drawHeadCSS(); $head .= $this->_drawHeadJS(); $head .= "\n</head>\n"; $head .= "\n<body class='wordpress'>"; $head .= "\n<div id='wrapper'>"; echo $head; } // And so on, including the CSS, JS, etc methods } And in practice: session_start(); include('include/everything.inc'); $user = $factory->getObject('user'); if(!$user->logged_in) { Header("Location: login.php?redir=home"); exit; } $page = $factory->getObject('page'); $page->setPage($_GET['page']); $page->drawHead(); $page->drawMenu(); $page->drawHeader($user->fullname); That code is obviously incomplete as I haven't included the <body> of the page yet. But that's my basic idea. I try to be of help, not sure if I am Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.