PHP Project Planning

Source: Internet
Author: User
Tags smarty template
Translator: Project planning is a topic that the industry pays more attention to, but it seems that there are not many articles about PHP project planning. In PHP freaks see this article, although not written professionally, but in relative Chinese resources relative lack of pan and lag of the PHP field, still have a little reference value. In the translation of the original language organization is not quite in line with the Chinese reading habits, so the original text of the logic of a certain modification. Resource sharing, welcome reprint, hope to indicate the source.

Original: http://www.phpfreaks.com/tutorials.php?cmd=view&tutorial_id=135

The original text of this blog archive: http://blog.donews.com/phpor/articles/460809.aspx

Author order: Most of the PHP programs I've seen have problems with architecture and organizational clutter, which means they lack planning. Many PHP programmers do not have enough consideration of the project to get into the coding work. This is an article I wrote 3-4 months ago, and I learned more, especially about OOP technology.

Planning?

Whether we are developing large, medium or small projects, planning is always the most important thing. When our levels reach a certain level, we can write programs without looking at tutorials or books. Maybe we know how to connect the database with PHP, output the results of the operation, create classes and so on. However, when many people reach this stage of their level it is easy to just encode and completely ignore the plan.

Although improvisation sounds good, planning is the best option if we want a tool to help streamline coding, easy upgrade, and easy coding. If we have already planned the database structure, if we have a rough outline of our own code, if we know very well what our program is for, then coding is just a piece of cake. Most books or tutorials do not explain the planning process in detail because the author is usually already doing the job. But if we want to make a program of our own, then the programming of the program is particularly important. When we want to add new features or features to a program, and to develop a new version, we find that program planning and good code structure are the most important aspects of the development process.

This article will take a message this program as an example, but will not teach you how to write PHP code, but rather teach you how to better organize and plan your code. It explores program feature planning, class planning, database structure, templates, message extraction, and some basic coding techniques.

Directory

First, take a look at what this article probably includes.

Planner Features

What is your program for?

What is the relationship between the various features of the program?

What code is used to handle the relationship between these features?

Database

What kind of database do you use?

How do I use the database?

What exists in the database? What exists in the config file?

Plan your Code structure

What classes do you use for your program?

What are these classes for?

How do you organize them?

How does the program call these classes?

What is written into the class and what is written in the process code?

Although the above is not the entire content of this article, but the article will discuss these issues.

Program feature Planning

The most basic requirement of planning program features is that we have to know what our program is for. First of all, to the basic features of the program has a general outline, like this message book, we can list the following things:

1) Display message function, 2) post message function; 3) management function; 4) template function.

How are these features written? Very simple, I refer to the other message book, to see what their characteristics, and probably did a classification. And then think about what types of features they don't have and what we want to add. But it's just a rough sketch of some basic features.

Now, we need to supplement and expand these features to list the detailed features we think the program must have. Here are some examples:

Display Message function

Default shows 10 messages per page, but allows users to customize their own

Use Java script to implement message expansion and contraction

Support BB Code

Post Message function

On each page add a link to leave a message

Information that must be filled in: What is said, message

Optional information: email, website, im account, location

Hide form fields: IP address (to prevent spam)

Management functions

Edit Message

Delete message

Shielded IP Address

Word Filter

Basic settings: Website name, message name, website URL, database host, name, user, password

Message reply

Simple template editing and add-on features

Administrator authentication

Template Features

Extract variable values from a database or text file and replace

If you don't want to write your own template, you can use a ready-made template engine like Smarty and pattemplate

Of course, the above list does not mean that we have planned the program, but have you found that the basic features are listed, the coding work becomes simple? When we start writing code, we know exactly where we're going, and the connections between the various functions.

Coding planning

Now it's time to start planning your code. Do we want the program to run based on the database? If so, what database is used? Or are we trying to make the program based on a text database? If so, how can it be achieved? And, what kind of structure is our code based on? For large-scale program development, my suggestion is only three words: Object oriented programming.

The best code structure for developing large PHP programs is using OOP. For example, we have a forum program that is developed using OOP. Now there is a class in the program called View, in view there is a method called Viewthreads (), you can accept parameters to output the content of the post. If there is now a page called viewforum.php to display a specific post each time, then we can call $view->viewthreads () to output the result. If there is also a search function to output the content of the post, then we just need to call Viewthreads () this method. Without OOP, when we were going to change the code of the post, we had to modify every file that displayed the post, but with OOP, we just had to modify the class code. But the real idea of using OOP is to make your code easier to organize and extend.

Because, we decided to use OOP to program, at least also to use in this message book. Now the question is: what classes are we going to write? What do these classes do for a job? If we are going to use a database, then at least one class is needed to handle the data exchange with the database. What's more, we need a message processing class to process the output and add messages, a management class to complete the management work, a template class to process the template. In some large projects, I recommend using out-of-the-box template classes such as Smarty and pattemplate, which can eliminate a lot of tedious work and can implement some powerful functions in the program, the most important thing is that you are now writing a message book, not the template engine, So the use of ready-made is possible.

When we decide which classes to write, it's best to list the classes and their methods. Here's an example:

Code
Class Entry {
function Entry () {
This method defines and initializes global variables and classes to include database processing
Note that this method name is the same as the name of the class, so that when the class is called, this method is also executed

}

Function view ($num, $start) {
This method will start reading the $num message number from the message in the database ID $start
}

Function post ($name, $email, $website, $aim, $yim, $msn, $icq, $title, $post) {
This method will write the transmitted data to the database.
}
}
?>

Obviously, a lot of coding is needed to complete these classes, although the code is not yet filled in, but we already have the approximate structure of the code, which makes it much easier to work on the coding later.

This example does not complete the whole planning work, but also does the same work for other classes. But this example tells you how to do it. Whether it is a large CMS, a forum, or a small message book, this practice can make your code better written.

Ways to make coding work easier

Template

In the beginning of programming, you must not ignore the template and template mechanism. All coding must be done in the template, so don't put it in the end. I recommend using the Smarty template engine, of course pattemplate is also good. But whichever you choose or write on your own, put this work first, because the template is the most relevant part of the end-user relationship. I recommend that you create a class that is responsible for managing the template, which is not a template to do, just to manage it. If we use the Smarty template, we can do this:

Code
Class Template {
function Template () {
Require_once ("smarty.php");
$smarty = new Smarty ();
}

function Showtpl ($TPL) {
$smarty->display ($TPL);
}
}
?>

To improve the management of the template, we have to do more work. But the above class is enough to allow you to simply reference the Smarty call and output code on the first page without having to write it again and again.

Abstract processing

What is abstract processing? What we often hear is "database abstraction," a technique that allows you to access many databases without having to modify the code. However, the so-called abstract processing can also be easily coded pronouns. There is a situation where we need to have the same content for all of the pages, such as headers, footers, variable references, and so on, so that we can rewrite the content over and over again, or you can create a page that includes the duplicate content and then reference it on each page. Although this method of referencing public files when outputting page duplicates is overshadowed by smarty, there are other uses, especially in large programs.

Message abstraction is an example of an abstract application. In this message, we need to output such as "message submission Success", "Please enter the name" and other messages, you can create a message class to handle it:

Code
Class Msg {
function msg ($num) {
$start = '

';
$end = '

';
$message = $start;
Switch ($num) {
Case 1:
$message. = ' Incorrect account name ';
Break
Case 2:
$message. = ' Wrong code error ';
Break
}
$message. = $end;
Echo $message;
}
}
?>

If you do a landing page, we can access the message class like this:

Code
Include ' msg.php ';
if ($pass! = "Arr") {
$msg = new MSG (2);
} elseif ($user! = 1user1) {
$msg = new MSG (1);
}
?>

Obviously, this is not a good example of authentication, but we can see the benefits of message abstraction through it. I have introduced this example into the article in order to make programming more convenient. Using a similar message abstraction mechanism where you need to output feedback, you can easily modify a file to complete the relevant work.

Not only that, if we need to add a new message, we can just put a new case, and then call it in the relevant place without adding the echo statement. If we use the template, we cannot simply put the message in the class as above, but when the class is called, using the pre-created template to display the message, this method is just three lines of code in Smarty.

Now you should be aware of the above-mentioned abstract processing.

One of the most important points to remember is to add the appropriate indentation to your code. The first method in the class, the first custom function in the program, the loop statement, the expression, and so on. In addition, annotations cannot be ignored. We certainly know exactly what the code we've just written is, but after a few months, we might want to upgrade the code, and then we'll find that the annotated code is actually much easier to read.

Planning!

After reading this article, you should know that what I have been emphasizing is planning, which makes coding easier. If the project is planned, all progress will be smooth. For example, if your specific functionality creates classes, debugging them will be a simple task, and if you use a template engine, your code will be cleaner and easier to maintain and upgrade.

Once you have determined the way you work, use these methods on every piece of code. Every time I plan for a project and use the right working methods, I find that the quality of the coding has gone up a leap. Maybe you don't want to put your time on the plan because it takes up your time, but when you want to add new features, upgrade the program, modify the code, or change the interface, you're going to be a pain in the face.

After reading this article, if you are interested in planning, you can try to list all aspects of the program like the examples given above, then write some simple programs and try the Hidden/shown features based on OOP and using Smarty. If you don't understand OOP programming, don't worry, you can read some tutorials first.

This article teaches you some basic ways to plan your project, and it's clear that this is not the only project planning effort. If you also want to understand the design of the database, you can see the lamp tutorial, in addition, this site also has the above mentioned message abstract tutorial. In any case, planning is really helpful to your work and hopefully this article will help you.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.