Analysis on developing large Web applications using PHP

Source: Internet
Author: User
Tags perl script
Analysis on the development of large Web applications in PHP, reading the analysis on the development of large Web applications in PHP, there are a lot of articles on the Internet about the execution efficiency of PHP, mostly the debate between the PHP and Java camps; different applications have different execution environments, and the efficiency varies greatly. The "large" applications mentioned here are not specific to large websites such as Google, eBay, and Yahoo "> <LINKhref =" http:

There are many articles on PHP execution efficiency on the internet. most of them are disputes over PHP and Java. different application aspects and different execution environments lead to a big difference in efficiency. The "large" applications mentioned here do not mean the implementation of large websites such as Google, eBay, and Yahoo. they just hope that their systems can run faster and smoother, it can host more users online, hoping to help PHP beginners.

The "large" application mentioned here does not mean the implementation of large websites such as Google, eBay, and Yahoo. I have no intention to persuade readers to give up their concepts and beliefs, I just hope that everyone's system can run faster and smoother, and more users can be hosted online. I hope this will help PHP beginners.

There are many articles on PHP execution efficiency on the internet. most of them start with the debate on PHP and Java camps and end with an uncertain expectation, with few clear conclusions. Indeed, the execution efficiency of the program is difficult to draw from the comparison. Different applications have different execution environments, and the efficiency varies greatly. Efficiency also needs to be weighed. everyone knows that the assembly language is very low-level and can write very efficient programs, but I still have very few. I should say that I have hardly seen anyone using assembly for Web development, in addition, those who have the ability to write efficient programs through compilation seem to be worthy of your attention. haha ~ We don't have to discuss the difference between PHP and assembly, as long as we know the difference between our PHP and other PHP.

First, we need to clarify the premise of this article: there must be one or more servers that can be manipulated by ourselves, rather than virtual host space. After all, the general systems that can run on virtual hosts already have a lot of classic works and mature frameworks, and efficiency mining has been done very well by our predecessors, many of their ideas have been inherited and developed by many PHP users. more and more so-called "frameworks" are like the stars, and I don't want to write any more, because first, my own level is not very good, I can't write any new things, and I am also smiling. second, I wrote too many articles and said too much, chaos is the biggest culprit in the death of many passionate talented programmers in the future.

Programs executed on independent servers are significantly different from those that can run on virtual hosts in terms of efficiency optimization. Of course, you can install a set of discuz on one or even a bunch of independent servers without modification. however, is it really the best performance optimization? are you really worthy of this pile of servers?

An independent server means that the user has full control over the machine, including installing and deleting software, configuring system parameters, and even modifying source code. Based on such an open hardware platform, the performance is not only reflected in speed, but also includes security and stability. Unlike virtual hosts, users must configure Web server parameters, install and configure PHP, databases, and install various messy things (which I like to say) on their own, and of course be responsible for them.

First, several terms are proposed: execution time, Template, database encapsulation, Cache, Buffer, Hash, Daemon, and crontab.

The execution time, everyone knows, is the time from the execution of a program to the end of the execution. Because the Web is instantaneous and stateless, the execution time is an indicator of the Web program execution efficiency. it is not suitable for measuring the C/S program or the background daemon program, because many of them are continuous. A typical example of page execution time is the explicit time at the bottom of the Discuz forum page. usually Discuz is several milliseconds to dozens of milliseconds, which is related to the platform, data volume, and current system pressure.

The template is familiar to everyone. although many people are only using it, they do not know why it is used. Traditionally, a template is a way to divide the logic layer. in the MVC structure, it separates the presentation layer from the lower layer. in actual use, it facilitates the division of labor between programmers and interface designers. However, in many cases, due to the improper use of templates, it does not promote the division of labor between programmers and interface designers, it turned out to be a hate affair between programmers and artists (as I have said in my previous post). many people complain that they have to spend a lot of time organizing templates.

Database encapsulation seems to have a greater relationship with Java. It provides a unified calling interface for multiple database systems, usually encapsulated classes, these classes sometimes complete some work, such as SQL check and filtering. The DB encapsulation, pear db, and Adodb in PHPLIB are well-known and widely used.

Cache and Buffer seem to be one thing. Cache is called Cache and Buffer is called Buffer. In the hardware concept, Cache is used to connect two devices with different speeds, such as registers and memory, CPU and PCI-Bus, IDE Bus, and hard disk. Buffer is a kind of Buffer similar to spring, which is used to reduce or absorb the shock. Buffer is a data pre-access method used to temporarily store data and transmit it at a different speed than the receiving speed. The Buffer update method can be automatically refreshed by time interval, while the Cache focuses more on the "hit rate". it puts a small amount of data frequently used in the current time period into high-speed devices for reading and writing. In program development, although there are no high-speed or low-speed devices, data sources can have different read/write efficiency. For a small amount of data, the reading and writing of text files is usually better than the database access efficiency, but also the reading and writing of text files, the efficiency on tmpfs is better than the direct disk IO efficiency. Buffer is more embodied in the process of communication and queue. in many cases, it is not because the receiver is not capable of reading faster, but does not need to read faster.

A Daemon is a program that runs continuously in the background. it usually monitors, controls processes, and provides external services. For example, Apache itself can be understood as a daemon, although it is actually composed of many frequently updated processes (the main process is fixed ).

Crontab is a UNIX/Linux timer program. it is a bit like a "scheduled task" in Windows. it is set to run a specific program after a certain interval or a certain time point. It is usually used to automatically update and clear temporary data once.

Another special concept (especially for those who are used to general system development) is that when we have an independent server, there is no need to limit ourselves to the functions that PHP can provide. when we become the masters of the system without knowing it, we should try to find this point, we have a lot to use. PHP is not omnipotent (this is definitely true). for its functional shortcomings, Perl can be used to make up for it. Perl is a common language, more functions can be provided. the modules with the same gravel are provided with endless energy for this random and abnormal language. You can use C to supplement PHP performance deficiencies. PHP is inherited by C. PHP itself is also developed by C. It is completely reasonable to use C for PHP extension.

Linux itself is supported by C and Perl (I didn't mean to exaggerate the position of Perl at all. you can check the number of Perl scripts in a standard Linux, after leaving Perl, does the system feel like a disabled person ). PHP inherits most of the syntax from C, I learned most of the Web features and functions from Perl and the "$" symbol that seems to be in conflict with open source (PHP was a Perl script in the early days ).

The following describes some of the code I am using (note: Linux independent server is applicable. I seem to have abandoned large-scale development for Windows and virtual hosts for a long time ). It uses some methods that may be familiar with, maybe unfamiliar, or abnormal. My system is RedHat AS3 and there is nothing special about it. The PHP version is 4.4.0 and the MySQL version is 4.1. I have never deliberately written code that must use the new features of PHP5, unless it is really necessary.

My Web root directory is at/www. Apache and PHP are installed at/usr/local/by default, and MySQL is the compiled binary version, I also leave it there. Because it is only used for testing, I don't want it to look messy. in actual projects, especially when there are multiple servers, you need to deploy your system properly.

To make the system structure clearer, I put all the files to be used under the level-2 Directory.

Below are some pieces of the common header file/utilities des/kernel/common. inc. php:

<? Php
If (! Defined ('in _ BSG ')){
Exit;
}
? >


The code above ensures that it can only be called by valid programs, rather than being included by other files. If the program being executed does not define a 'In _ BSG 'constant, it terminates the program after including this common. inc. php.

<? Php
List ($ usec, $ sec) = explode ("", microtime ());
$ Page_time_start = $ usec + $ sec;
? >


You may be familiar with these two lines, which is the start time of the computing program. Before the program ends, we will calculate this again to get the time required for program execution. If you don't care about this, you can comment it out with confidence.

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.