Developing robust code series articles with PHP is about solving practical problems in large and medium-sized applications. This series of articles focuses on the new functions available in PHP4, and focuses on a lot of tips and tricks to make development easier. In this series of articles, you will find a lot of examples and technologies to learn, and a lot of sample code is attached. In this first article,
Develop robust code with PHP This article is about solving practical problems in large and medium-sized applications. This series of articles focuses on the new functions available in PHP 4, and focuses on a lot of tips and tricks to make development easier. In this series of articles, you will find a lot of examples and technologies to learn, and a lot of sample code is attached. In the first article, PHP expert Amol Hatwar introduced how to design and compile error-free and maintainable code for medium to large Web applications from a higher perspective.
If you are a Web application developer and need platform-independence, PHP will be suitable for you. Moreover, PHP is free and easy to learn and deploy. These are the biggest advantages that make PHP so popular. However, these advantages can also become disadvantages. Because PHP is easy to use, developers often put the code into the editor before planning and designing it. Moreover, in PHP, there are more than one solution to the problem, which is easy to make that painful mistake, and it is difficult to correct it later.
In this series of articles, you will learn how to avoid many mistakes. If you do not stick to it, you will find that you can write error-free code once or twice. Don't be surprised. I will also point out the new features available in PHP 4 that make development easier. Most examples I will introduce are used to handle actual problems such as script configuration and installation, file processing, and database usage. Even if you are not familiar with all this, you will find it easy to understand. However, I suppose you have a preliminary understanding of PHP. If you need to study again, you will find that the references listed at the end of this article will be helpful to you. (SeeReferences.)
Lay a strong foundation
Writing code in PHP is very similar to writing code in a language similar to C. Because they are syntactically similar, similar code maintenance problems may occur. When you need to develop large applications, you may need to write a lot of code. As time passes, these codes may become difficult to manage, and errors will soon be passed in. Do not believe this statement wherever you hear it-at least not all. However, the more important fact is that if you find that your code is maintained too frequently, your application may be poorly designed first.
Correct code design
Your initial choice affects your freedom to write code later. This makes correct design an important prerequisite. Although the design will become an additional ceremony when you solve a trivial problem, you must at least know that it should be done. Many people regard design and planning as additional expenses. But starting with a bad design, or there is no design at all, the results will always lead to sloppy code. Remember, clever coding cannot make up for bad design. Although how to design applications is beyond the scope of this series of articles, I will still be prompted with some questions that should be kept in mind when designing Web applications.
Split and conquer
Designing and writing large applications with small loosely coupled components is always more desirable. In this way, each part is maintainable. For example, a Content Management System (CMS) may be distributed in many smaller modules, such as user authentication, display, Content parsing, and statistics viewing. In addition, if your module is common enough, you can reuse the code in other applications you develop. Programmers often do this, but they still complain. The coding technique is to make each module have the functions that it absolutely requires and so far. It is just right to assign more functions to a module.
Never consider
If you are interested in using PHP to make your HTML page more dynamic, this part will be suitable for you. When you want to display the date and time on each page to make it look the latest, it is usually the beginning of this habit. Each HTML page is converted into a small PHP script with only a few lines. the date function is hidden somewhere. If this is what you want, you must acknowledge that this is the easiest way to implement this job. However, imagine what changes you must make to change the date format displayed on the page. You will have to change the code on each page.
Obviously, there is a better way to achieve this goal. My favorite method is to use the configuration file and define a constant in the configuration file. This constant is retained.date()
The format string of the function. Then, you can usedate()
Function. Each page still ends with a script, but you need to minimize the places where you must make changes to a single line of code.
As shown in, you can see the date format changes on all pages. The idea here is to avoid copying code and hard coding. Keep this in mind when writing large applications. Debugging and maintenance become easier if you do not copy the code.
Reduce client requirements
The types of Web pages and applications have been evolving in multiple directions. The first is image and image ing, and then the Java applet and client scripts that implement cool animations. It is Flash. An influence is a good thing, but you must remember that the overall idea behind the Web is to allow anyone who needs the information to access the information at any time. If you use a technology that is not supported by all browsers and platforms, people will be rejected to access your information. You never know whether people who cannot access your website can become your next customer!
Your application design should minimize network traffic as much as possible. We often see that many websites have too much traffic and only provide browsers with cookies that can only run on them. Tampering with a large number of cookies not only consumes bandwidth, but also makes it difficult to manage many aspects. Based on experience, if your application sends more than 40 KB of data or if your page takes more than 5 seconds to load, you should design the application from the beginning. Small adjustments will not last long. If you look around, you will find that the most popular and most visited sites are very simple.
You should also consider the next wave of mobile devices and the connections they use to access your applications. The best practice is that your application must adjust the content based on the client of the requested content. Leon Atkinson in hisCore PHPProgramming"We can manage to reduce the size of HTML documents and avoid complicated HTML such as nested tables, however, we cannot upgrade every 28.8 modem."
Separate code, content, and display
HTML is the markup language for displaying content, and PHP is the scripting language embedded in HTML. This makes simple tasks easy-for example, taking a date as an example. However, if you want to implement complex requirements, Embedding PHP in HTML will make the code simple. It is good to separate code, content, and display as much as possible. Consider the document you are reading. This document is created as an XML file. HTML and PDF versions are automatically generated using style sheets. Code (the application that shows you the page), content (XML document), and display (style sheet) are different.
Just as it is important to encapsulate core functions in modules and avoid code replication, it is also important to use separate content source code and display content as required. The flexibility of customizing pages based on the client and connection speed is also increased. At the same time, it is a good thing to make programmers, designers, and authors work independently of each other-if you are working on a large project.
Don't be afraid to abandon design
No matter what others tell you, practice is the best way to learn application design. If you are just getting started, you may make many mistakes-this is the learning method. Poor design should be abandoned. That's why you must keep code, content, and display loosely coupled-abandon bad design as a means to ease the pain. When you discard old code and replace it with better code, you can retain the content and display.
Now, go back to the issues I have promised to make your code robust. You must always remember that your code will provide strength for the content and display of others. If your code is not competent, other departments will not be able to make up for this defect with amazing efforts.
Compile robust code
Assuming that your code requirements remain unchanged, you will not obviously find the need to change the code. In addition to occasional optimization and improvement, your code should run like a machine with lubricating oil.
It sounds very difficult? This is not the case. Frankly speaking, writing robust code doesn't take a long time. You only need to ask yourself some appropriate questions when you are not sure, so that you will not be on the right track:
- Is it safe?
- Is it simple and easy to understand?
- Is it Platform independent?
- Is it fast enough?
Protect your code
Any system that satisfies a large number of users must be secure. Although PHP itself is not vulnerable to black-hat attacks, do not be too certain. PHP 4 earlier than version 4.2.2 has serious security defects. Always ensure that sensitive data is stored or transmitted on an appropriately encrypted network. This is more important for applications that process business, store information (such as credit card numbers), and transmit confidential data.
Currently, it is difficult to trust data submitted by users. Ensure that the data is verified and that the data is clean before use. Keep in mind that putting your Web applications on the internet means publishing your systems, software, data, and business to a huge network.
Make sure that your code runs securely all the time.
Keep code simple
Your code should be easy to understand, readable, and well-documented. To reduce the time required for you to familiarize yourself with the code of yourself or others, always use public naming and coding conventions in the project. Please invest time to ensure that these aspects will return when you need to maintain the code.
You 'd better create a document for the code during programming. Tools for parsing all scripts and creating neat HTML documents do not exist. If you change the code behavior, you need to change the document accordingly. If the document of the code does not actually record anything, it is useless to have such a document.
Make sure that your code is documented, simple, and easy to understand. In the long run, this will be helpful.
Make the code platform independent
Another issue that you must solve is platform independence. Of course, scripts written for PHP on Windows will work for PHP on any other platform: PHP is designed like this. However, you still need to be careful about small inconsistencies. For example, line breaks are represented in different ways in Windows and UNIX.
You must also extract data when accessing resources (such as databases outside PHP. For example, your application uses MySQL as the database server to cut costs. If you decide to have a more functional database in the future, you must change the code in the application. Making major changes to the code is always a task and an error-prone process. Use extraction to isolate changes that are easy to change. You do not have to rewrite the entire application.
Make sure that you write platform-independent code. This gives your application better adaptability and scalability.
Build for speed
The last factor worth processing is speed. No one is willing to wait until your script selects about 300 entries from the database and displays a page. It is usually a good idea to place 20 results on 15 different pages (these pages are quickly moved and loaded on the user's browser. The user regards a faster response time as a speed. Another easy mistake is to dynamically create a page every time a user accesses the page. This ensures that your site is always up-to-date, but PHP cannot accept this when the number of users increases. You should cache frequently-used pages. High-speed cache makes your applications faster and reduces the load on the server.
Make sure your code runs quickly. Nobody likes to wait.
Summary
In the first article of this series, you learned what to do in writing robust code. If you study it carefully, it is not difficult to develop large applications using PHP. In fact, many applications written in PHP have been easily developed. At the same time, it is important to realize that you are easy to fall into the trap. As long as you plan, break down things into many smaller tasks, and implement them correctly-there is almost nothing to block you.
In the next article, you will learn how to efficiently use variables and functions. I will show you how to change the variables and functions in PHP.Magic. In this article, we plan the main content we will discuss. After the next article, we will discuss the content one by one-and may even jump into the story. Goodbye.
References
- Please visit phpbuilder.com to obtain a large number of beginner-to-intermediate tutorials and references.
- Improve your skills through Zend discussion groups, articles, and many other techniques.
- Learn the Developer Shed tutorial and learn how to use PHP to compile self-documenting code.
- Try to extract documents from your file using the PHP compiler entor tool written in PHP.
- Explore many beginner's guides on Weberdev.com to get a message about using PHP.
- Start PHP now? Please read "Introduction to PHP", a good article to start learning PHP (DeveloperWorks, January 1, December 2002 ).
- Please read "Script Web databases quickly with PHP scripting language" for an excellent introduction to bringing PHP and database together (DeveloperWorks, January 1, September 1999 ).
- Through the article "Creating dynamic Web sites with PHP and MySQL "(DeveloperWorksIn May 2001, I learned how to use PHP and MySQL encoding.
- Use "Personalized Web sites in a jiffy "(DeveloperWorksSeptember 2000) find more places to apply your new PHP skills in reality.
- Read Kent Beck's eXtreme Programming Explained for an excellent introduction to XP.
- Refer to Leon Atkinson's Core PHP Programming, a Practical PHP 4 Guide for Web developers.