Jump to content

Assigning a huge string to a variable - is this acceptable practice?


cowfish

Recommended Posts

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=

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.