PostWorlds, Viewing Pages, PHP Classes

I looked at this giant post about to hit Facebook and thought it might be better as a blog post.  I’m working on a Play By Post site in the style of the site I once loved, and spent a while working on how to view a page that had been stored in the database.  There were multiple “hard way”s to write one of the pages.

All of the code here is very simplified from what I’m trying to (except the HTML examples), and the variable names are not the same as in my real code.

The easiest way was to intermingle the programming language and the HTML/web page language at every step of the way.  The PHP scripting and HTML code would be so intermingled that none of it could be easily re-used, or even understood after any time has passed.  I’m not even going to write that here.  It would have been a piece of script to open the database connection and connect to this “row” of data, followed by enough HTML to get to where the next piece of data is needed, followed by PHP script to retrieve that piece of data alone, followed by enough HTML to get to where the next piece of data is needed.  Everything would have been horribly tangled up, and a pure nightmare to look at. 

Another way was to write a relatively easy/straightforward function. The function would collect all of the data and make it available to an array – or a list of pieces of data.  To the best of my knowledge, it would have returned a numbered array. So, i would need to remember and/or document something like:

$pagepart = array();
$pagepart = GetPageFromDatabase($pageid);

Then I need to remember that

$pagepart[0] /* is the title. */
$pagepart[1] /* is the "body" of the page. */
$pagepart[2] /* is any unique stylesheet information. */
$pagepart[3] /* is the maximum number of new posts to show on the front page. */

There are, at present, fourteen pieces of information that would have to be kept track of this way.  The finished code would look like:

<html>
 <head>
 <title><?php echo $pagepart[0]; ?></title>
 <link rel="stylesheet" href="<?php echo $pagepart[2]; ?>" />
</head>
<body>
  <?php echo $pagepart[1] ?>
</body>
</html>

That’s better, but it’s going to suck if I want to edit it a year later.  I have yet to get a function in PHP to return an array with sensible keys.  For some reason arrays with sensible keys bother the hell out of me when classes exist. Plus, learning to use classes is learning another skill to work toward a better future.

So, I could use classes. Now I can just access something that looks more like

$pagepart = new PageInformations(0,0,0,0,$pageid);
$pagepart->PopulateFromDatabase();

Later instead of trying to remember if $pagepart[0] is the title or style or body of the page, I have a nicely named piece of data.

echo $pagepart->title; /* is the title */
echo $pagepart->body; /* is the body of the page */
echo $pagepart->style; /* is the unique style information */
echo $pagepart->ShowThisManyPosts; /* is the maximum number of posts to show on the front page */

Now everything piece of data has an obvious name instead of an obfuscated secret agent number. This should make editing the whole thing easier months or years later when it’s not fresh in mind/memory. It also does a nice thing separating my programming language code mostly away from my “make the web page” code.

With all of the programming code written on the top of the page, the rest can resemble:

<html>
 <head>
 <title><?php echo $pagepart->title; ?></title>
 <link rel="stylesheet" href="<?php echo $pagepart->style; ?>" />
</head>
<body>
  <?php echo $pagepart->body ?>
</body>
</html>

There are regrettably other times when the PHP scripting / code need to be much more entwined with the HTML/web page code than this.  Whenever possible, I want that last example to represent the bulk of my code.  The scripting is done first.  HTML code is written next, only calling a small piece of scripting code when something is needed.   That, for the most part, should keep the programming with the programming and the web page with the web page.  Then if there’s a programming error I’m not searching through page code, or if the page does something weird like align itself to the right in a 20-pixel wide DIV, I won’t have to dig through very much PHP scripting to find out why.

Andrew
Latest posts by Andrew (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.