Improving Our Application
Sep 16th, 2007 by jon
Originally I had intended for this post to include building a simple Content Management System (CMS) using object oriented programing and a mySQL database back end, but I considered that some of my readers who have never used these technologies before may become confused. So instead we will be taking some baby steps and making some small improvements to the application we built in the previous post.
The first improvement we are going to make is changing the datafile. The old datafile was a comma separated value file which would cause some serious problems for anyone who was actually going to try to blog with our simple application. So the first thing I did was replace all the commas with &^&. The &^& symbols would almost never show up together in a blog post so they are a pretty safe bet if you want to use text files to store your posts.
Hello&^&09-04-2007&^&This is my first post!
Testing&^&09-03-2007&^&I am going to test my blog now!
The second modification I made to the data file was adding a “post order”. This number is the order in which the posts are displayed. This is **not** a good longterm solution but for training purposes it will work fine.
1&^&Hello&^&09-04-2007&^&This is my first post!
2&^&Testing&^&09-03-2007&^&I am going to test my blog now!
Our next step is to remove all the post displaying functionality from index.php and place it into a class. In this type of example a class is basically a collection or related functions. Classes can also be used to build objects. A good way to look at Classes and Objects is to think of them like cars. A Class would be the blue-print for a Honda Civic and an Object would be a Blue Civic EX or a White Civic SI. In the following code I have taken all the blog post related functions and placed them into a class called Post.
-
<?php
-
class Post {
-
function _getRawData($filename) {
-
-
return $raw_data;
-
}
-
function getPostArray($filename="posts.csv") {
-
$raw_data = Post::_getRawData($filename);
-
break;
-
} else {
-
$posts[] = "<p>";
-
$posts[] = "<strong>" . $post[1] . "</strong>
-
";
-
$posts[] = $post[2] . "
-
";
-
$posts[] = $post[3] . "
-
";;
-
$posts[] = "</p>";
-
}
-
}
-
return $posts;
-
}
-
}
-
?>
In the code below I have done the same thing as above except this class will store all the functions related to Layout. In a later example we can make the layout class more intelligent to all the user to change the theme and style of their blog.
-
<?php
-
class Layout {
-
-
$out[] = ‘<html>’;
-
$out[] = ‘<head>’;
-
$out[] = ‘<title>’ . $title . ‘</title>’;
-
$out[] = ‘</head>’;
-
-
}
-
-
$out[] = ‘<body>’;
-
$out[] = ‘<h1>’. $title . ‘</h1>’;
-
$out[] = ‘<p>’;
-
-
foreach ($data as $d) {
-
$out[] = $d;
-
}
-
-
$out[] = ‘<p>’;
-
$out[] = ‘</body>’;
-
-
}
-
function Footer() {
-
-
$out[] = ‘</html>’;
-
-
}
-
}
-
?>
Lastly we are left with a more simplified index.php file. While this is nowhere near what I would consider to be a “Simple CMS” it is still a good start.
-
<?php
-
include(‘./classes/layout.class.php’);
-
include(‘./classes/post.class.php’);
-
-
$out[] = Layout::Content(‘Simple Blog’, Post::getPostArray("posts.csv"));
-
$out[] = Layout::Footer();
-
-
-
?>
In the next example we are going to be removing the CSV file and replacing it with a mySQL database, as well as adding some fully object oriented programming examples.
If you like this post, then consider buying me a beer!