Tutorials to .com

Tutorials to .com » Php » Others » PHP development with robust code (a): Introduction from a strategically advantageous position

PHP development with robust code (a): Introduction from a strategically advantageous position

Print View , by: iSee ,Total views: 1 ,Word Count: 2880 ,Date: Mon, 20 Apr 2009 Time: 1:32 AM

Robust with php code development is a series of articles on the settlement of large and medium-sized applications to practical problems. This series of articles focusing mainly on PHP 4 in new features can be used to highlight a great deal of work to make it easier to develop the skills and know-how. In this series of articles, you will find an example of a lot to learn and technology, also comes with a large number of sample code. In this first article, PHP expert Amol Hatwar from a higher perspective for how to design large-scale Web applications and the preparation of error-free, maintainable code.
If you are building a Web application, a developer and need the speed, functionality and platform independence (platform-independence), then PHP will be for you. And PHP is free, easy to learn and deploy. These are to make PHP so popular greatest strength. However, these advantages can also become weaknesses. Because PHP is easy to use, so developers should be in the planning and design, they often first factor into the code editor. Moreover, in PHP, the solution to the problem more than one, relatively easy to commit the kind of tragic mistakes, such a mistake after the revised difficult.

In this series of articles, you will learn how to avoid many mistakes. If you insist on landing in the end does not step in, you'll find one or two will be able to try to prepare error-free code, and this should not be surprised. PHP 4, I will point out that the new features available in their development work to make it easier. I would like to introduce most of the examples are used to deal with issues such as configuration and installation scripts, document processing and database such as the use of practical problems. Even if you are not familiar with all this, you will find it very easy to understand. However, I assume you have some basic understanding of PHP. If you need to re-study, you will find this paper, the premises at the end of the reference list will help you. (See references.)

Lay the foundation for robust
Using PHP to write code and use similar language c is very similar to writing code. Because of their similar syntax, it will lead to a similar problem of code maintenance. When must the development of large-scale application, you may want to prepare a large number of the code. Over time, these codes may become difficult to manage, will soon take advantage of an error. No matter where you hear it, do not believe that - at least not the whole letter. However, a more important fact is that if you find yourself too often to maintain the code, then first of all, your application design may be very bad.

Design code correctly
The first choice you make will affect you the freedom when writing code. This makes the design of the right to become an important prerequisite. Although when you solve a problem of minimal design will become an extra ceremony, but you should at least have to know is to do so. Many of the design and planning as the additional outlay. However, bad design or no design, the result would always lead to sloppy code. Please keep in mind, and then clever coding can not compensate for bad design. While how to design applications that goes beyond the scope of this series of articles, but I will be prompted to some of the design of Web applications should be borne in mind the question of when.

Segmentation and conquer
Through a loosely coupled smaller components to the design and preparation of large-scale applications are always more desirable. In this way, each component can all be maintained. For example, a content management system (Content Management System, CMS) may be found in many, such as user authentication, display, content analysis and viewing of statistical information, such as smaller modules. Also, if you sufficient general-purpose modules, you can develop in your other application code reuse. Programmers often do, but they are still complaining. As coding techniques is to allow each module with it is absolutely necessary and the end of the function. Given the function of a module to be no more, no less, right.

Do not consider the page
If you use PHP on your HTML page more interested in becoming more dynamic, it will be for you. When you want to display on each page the date and time so that it appears to be the latest time, usually beginning the habit. Each HTML page into a few lines only limited small PHP script, date functions in a certain place to hide. If this is you want, you must be recognized that the operation to achieve the most simple way. However, imagine what you must do in order to change the page, change the date format shown. You will have to change the code of each page.

It is clear that there is a better way to achieve this objective. My favorite way is to use the configuration file and configuration file in a constant definition, the constant reservation date () function of the format string. Then, you can use in the required date () function of. Script is still the end of each page, but you have to complete those you need to change a single line of code down to at least place.

Figure 1. To avoid hard-coding




The above chart you can see all the pages to change the date format. The idea here is to avoid copying code, as well as hard-coded. In the preparation of large-scale application, keep in mind this point. When you avoid copying the code, debugging and maintenance becomes more easy.

Reduce the client-side requirements
Web pages and the type of application has been developed in several directions. First of all, images and image maps, and then cool animation to achieve the java applet and client-side scripting. It is Flash. Impact is a good thing, but you must bear in mind the overall thinking behind the Web is to make any necessary information can access this information at any time. If you are using, not all browsers and platforms are supported by technology, people will refuse to access your information. You never know can not access your Web site whether the person could become your next customer!

Your application will be as much as possible should be designed to minimize network traffic. We often see many sites and too many visits to only one type of browser so that it can only run the cookie. Tampering with a large number of cookie consumption not only bandwidth, but also difficult to manage many aspects. Based on experience, if your applications send messages over 40 KB of data or page if you need more than 5 seconds to load, then the application of the design from scratch. Various small adjustments will not last too long. If you look around, you will find the most popular to visit the largest number of sites are very simple.

You also should consider the next wave of mobile devices and their applications to access your connection used. Is the best way, your application must be based on client requests for content to adjust the content. Leon Atkinson in his book Core PHPProgramming first mentioned in 720, "We can try to control the size of HTML files smaller, and that we can try to avoid, such as nested tables (nested table) such a complex HTML, But we can not on everyone's 28.8 modem upgrade. "

The code, content and display separate
Is to show the contents of HTML markup language, PHP is a scripting language embedded in HTML. This is indeed a simple task to make it easy - for example, as an example to date. However, when you want to achieve the requirements of complex, it will be PHP code embedded in HTML so that a complete loss of simplicity. As much as possible to code separately from the content and display it well. Think about what you're reading this document. This document has been created as an xml document. HTML and PDF versions of the style sheet is automatically generated. Code (the page to show you the application), which (XML document) and display (style sheet (style sheet)) are different.

As the core function and is packaged in a module is very important to avoid copying the same code, the contents of a separate source code, as well as be shown in accordance with the requirements of the content is also very important. You in accordance with the client connection speed and the flexibility of custom pages also increased. At the same time, so that programmers, designers and writers to work independently with each other - If you are dealing with a large-scale projects, this is a good thing.

Do not be afraid to abandon the design of
No matter what other people tell you, the practice of the application is designed to study the best way. If you just started, may be guilty of many errors - This is the way of learning. Bad design should be abandoned. This is what you need to maintain code, content, and show the reasons for loose coupling - discard bad design means to relieve pain. When you throw away the old code, so as to better the time to replace the code, you can keep content and display.

Now, I promised to return to the issue, so that with your code robust. You must always remember that your code will be other people's content and display strength. If your code can not do so, other sectors of the efforts of more amazing can not plug this loophole.

The preparation of robust code
Assume that your code requests the same, obviously you will not find the need to change the code. Apart from the occasional need to optimize and improve the outside, your code should look like the addition of lubricants to run the machine.

Sounds very difficult? This is not true. Frankly speaking, the preparation of the code do not need robust talent. Sure you only need to ask ourselves when the issue of appropriate so as not to deviate from the track:

It safe?
It is simple and easy to understand?
It is platform independent it?
Do it fast enough?
Protect your code
A large number of users satisfied with any of the systems must be safe. Although PHP itself susceptible to the black hat (black-hat) hackers, but you will not be too sure. In the version of PHP 4 before 4.2.2 have serious security flaws. Should have been encrypted to ensure that the appropriate network storage or transmission of sensitive data. This is business, storage of information (such as credit card numbers), as well as confidential data transmission applications, it is more important.

Now, it is difficult to trust the data submitted by users. To ensure that the data are verified and to ensure that data is clean before use. Keep in mind that your Web applications on the Internet is to publish a huge network of your systems, software, data and business.

Ensure that your code has been running safely.

To maintain the code simple
Your code should be easy to understand, good readability and good documentation. In order to reduce your own or other people familiar with the code of the time required for the project had been in the use of public naming and coding agreement. Please input the time to ensure that the code need to safeguard these areas will be rewarded.

You may want to code in the programming for the establishment of the document. Able to resolve all of your scripts and create a clean look of the HTML document format tool does not exist. If you change the code of conduct, it is necessary to change the document accordingly. If the code does not document what the actual record, then have such a document is useless.

Ensure that your code is available in the document, a simple and easy to understand. In the long run it would be helpful.

Enable the code is platform independent
You must solve another problem is platform independence. Of course, for Windows on the PHP script will be written on any other platform PHP function: PHP is one such design. However, you still need to carefully compare the small inconsistencies. For example, the newline characters in the Windows and Unix on that in different ways.

Your visit to resources (such as PHP is located in external databases) must also be taken to use. For example, applications that you use mysql as a database server to cut costs. If you decide later to have a more feature-rich database, you must change the application code. Substantial changes to the code is always a difficult task but a process of error-prone. Please use the samples to isolate the changes to the easy part of the change. You do not have to rewrite the entire application.

Prepared to ensure that your code is platform-independent. This makes your application has better adaptability and scalability.

Construction speed
Finally, a factor is the speed of processing. When your script from the database to drag to select approximately 300 entries and display a page, no one has been willing to wait. The results will be 20 on 15 different pages (these pages in the user's browser on the fast-moving and packed) is usually a better idea. Users to understand the faster response time for speed. Another mistake is easy user access for each page are dynamically created page. This is to ensure that your site is always up-to-date, but when the number of users increases, PHP will not be allowed to do so. You should cache pages that use frequently. Cache so that your applications faster and reduce the load on the server.

Ensure that your code runs fast. No one likes to wait.

Summary
In the opening article of this series, you learned about the preparation of robust code to actually do anything. If you seriously study the case, with the development of large-scale PHP applications are not difficult bit. In fact, many applications written using PHP procedures have been developed to easily. At the same time, realize that you easily fall into the trap is very important. As long as you have been planning, to bring the issue into a number of smaller tasks and the right to achieve them - almost anything else will not hinder you.

The next article, you will learn how to efficiently use the variables and functions. I will show you how the additional use of PHP variables and functions in the variable number of tricks. In this paper we are planning to discuss our main content. The next article, we will discuss these elements one by one - may even jump on the. When good-bye.


php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.