HomePHP Page 2 - Defining the Core Structure of a PHP Blogger
Defining the blogger's core structure - PHP
In the first part of this three-part series, we'll examine the basic outlines of creating a blog application with PHP 5. If you're currently working with PHP 4, this program can be adapted to that version of the language.
Naturally, a good place to start building the blogger system with PHP 5 consists of defining its basic structure so it will be easier for you to understand how each module of the application fits together with each other module.
This being said, the blogger in question will be created by using only one PHP class. It will be responsible for performing the insertion, update and deletion of blog entries respectively, in conjunction with displaying the entire list of blogs and the corresponding online forms, handy for posting and updating a particular entry.
Considering all the prerequisites that I established before, here is the initial definition for the new "BlogProcessor" class. Take a look at its signature, please:
// define 'BlogProcessor' class class BlogProcessor{ private $mysql; private $blogData; public function __construct(MySQL $mysql){ $this->mysql=$mysql; $this->blogData=$_POST; } // display blog system public function displayBlogger(){ // edit blog if($this->blogData['editblog']){ // code for displaying edit page goes here
} else{ // insert new blog if($this->blogData['insertblog']){ $this->insertBlog(); } // update blog elseif($this->blogData['updateblog']){ $this->updateBlog(); } // delete blog elseif($this->blogData['deleteblog']){ $this->deleteBlog(); } } // code for displaying main page goes here } }
As illustrated above, the skeleton of the new "BlogProcessor" class is very simple to grasp (at least for now). Basically, I split up the structure of the class into two main methods. The first one is obviously the constructor that performs a few useful initialization tasks, while the second one, called "displayBlogger()," is tasked with inserting, updating and deleting blog entries, as well as displaying the corresponding forms for handling blog-related data.
However, for the moment you should ignore all the processes concerning the display of blog entries, and focus your attention on the signature of the methods listed above. As you can see, the constructor accepts an object of type "MySQL" as its unique input parameter, which comes in useful for database connectivity inside the blog processor class, running queries and processing MySQL result sets.
Don't worry for the moment about this MySQL class, because at the end of the series you'll be provided with all the classes required to get the blogger working. However, at least for now, you'll have to settle for with studying the methods declared before.
Returning to the definition of the "displayBlogger()" method that belongs to the blog processor class, you'll realize that it contains three additional private methods. These are called "insertBlog()", "updateBlog()" and "deleteBlog()" respectively, and they perform well-differentiated tasks, like inserting new entries, and updating/deleting existing ones. So far, nothing unexpected, right?
In addition, you'll notice the use of the "$this->blogData" property as a flag for executing the different operations associated with distinct entries. In this case, I decided to use the superglobal $_POST array to do this, but this condition can be easily modified and utilize instead some variables passed in via the GET method.
All right, I believe that all the explanations that I offered before would be rather incomplete if I don't show you a few screen shots that illustrate the look and feel that I plan to give to the blog system. Therefore, here are the corresponding images:
As you can see, the three pictures included above demonstrate in a clear fashion the different screens that will be presented to the user to perform the insertion of entries, as well as the update and deletion of them. In addition, I showed you the panel that displays the complete list of blogs, which means that at this point, you should have an accurate idea of how the blogger system will look at first glance.
Well, having defined the general structure of the corresponding "BlogProcessor" class, it's time to move on and start coding each one of the private methods that were declared previously to perform the insertion, update and deletion of blog entries. Sounds interesting, doesn't it?
To learn how these important methods will be defined, keep reading.