PHP enthusiasts will love PEAR. Our purpose is to introduce PEAR to PHP program developers. I personally think that the program development method that references libraries has gradually become a trend. In addition to PHP and JSP, other programs such as perl and C++ are also There is a dedicated library for its developers to reference.
By introducing PEAR, PHP program developers can not only greatly reduce the work and time of writing code, but also obtain many powerful dynamic web page functions in a very simple way. I will list many examples to help After watching it, everyone will definitely applaud and become deeply fascinated by PEAR. In addition, the program code is easy to maintain and high security are two other attractive advantages of PEAR. The PAER program library is like a module and can be easily swapped and updated without affecting other program codes at all.
We start from the most basic concepts, including the meaning of the proper noun PEAR? Where to get the PEAR package? How to use the PEAR package, etc.?
Starting from Chapter 2, we first introduce The origin, history and characteristics of PEAR give everyone a basic understanding of PEAR.
In Chapter 3, we will introduce the installation and management methods of the PEAR library package. This chapter is very important. Before using PEAR, you must first learn these basic skills. Although it is a bit complicated, it will be useful after learning it. , doubling your PHP program development efficiency and increasing its functionality.
Chapter 4 uses a simple example to introduce how to use the PEAR library to develop PHP programs. It allows us to understand the general use of PEAR and establish a basic operation process for the use of the library.
In Chapter 5, we will introduce several examples of simple-to-use and powerful program library suites, allowing you to quickly create some very cool functions that you could not imagine before on dynamic web pages, making your program development The skill is improved immediately. For example:
◆ Allow the webpage to provide weather forecast functions and increase the added value of the webpage.
◆ Use a very simple method to create a cache of web pages on the server to speed up the opening of web pages.
◆ Search the dynamic web page for services that provide PORT numbers for a certain host.
◆ Query a domain name (DNS) on a dynamic web page and find the corresponding IP.
◆ Dynamically generate HTML tables.
◆ Quickly establish a "customer reply" system, allowing web viewers to send letters to webmasters from the web page.
◆ Allow web pages to record the behavior of web browsers, that is, log.
From Chapter 6 to Chapter 13, we will introduce in detail how to use some important, commonly used, but more complex library packages. These chapters are all based on packages as the main introduction, that is to say , one chapter only introduces one package, in order to introduce the more important functions of these important packages to everyone in detail.
These packages include: database connection package, form production package, identity authentication package, file upload package, paging package, etc.
Finally, Appendix A will give a detailed introduction to the official website of PEAR. When you enter the world of PEAR, you will definitely come to this website frequently to look for information. Knowing how to use the official website is very important for PEAR PHP developers. Appendix B will give a brief introduction to PHP's object-oriented programming.
Those who like and are suitable to use PEAR are classified as follows:
◆ PHP lovers, you must use PEAR, because it is absolutely awesome.
◆ Professional PHP programmer. What you need is code security, reproducibility, and good work efficiency. PEAR is an absolute must.
◆ PHP programmers who want to develop more interesting and more powerful applications, but don’t want to be tied up in some troubles of writing PHP programs, PEAR can definitely meet your requirements.
◆ If you write PHP programs yourself, but don’t like to start from scratch every time and type the code word by word, PEAR can improve your work efficiency.
◆ If you want to learn PHP with strong functionality and high security, after you have learned the basic syntax of PHP, you can try to use PEAR.
◆ You need a tailor-made PHP application, in other words, you don't like packaged applications like XOOPS, try PEAR.
◆ You need a PHP application that is easy to maintain, and you don’t want to modify the code frequently in the future, or redesign the entire program due to PHP revisions.
◆ You want your PHP program to be object-oriented to facilitate program swapping and modularization.
◆ If you want to take your PHP skills to the next level and learn more PHP-related knowledge, using PEAR or watching the source code of PEAR is a good way to learn.
PEAR is specially written for people with the above needs.
Using PEAR is like building a house. It allows you to use prefabricated components to build a large house that you design and construct by yourself, but you do not need to mix cement, prepare sand, stones, or bake tiles by yourself. Make your own aluminum doors and windows, do your own carpentry decoration, etc...
PEAR is like prefabricated parts that have been customized and prepared according to your needs. You just need to use creativity to combine them.
When we use PEAR to write PHP programs, we must have a prerequisite, that is, we must have a certain understanding of PHP-related knowledge.
If you don’t know what PHP is, what Apache and Mysql are, or even though you know it, you don’t know the basic syntax of PHP, then this article is not suitable for you at the moment. It is recommended that you buy some introductory books on PHP, Apach, and Mysql and study them in detail before reading this article.
You don’t have to be a PHP master, but you must have the ability to understand basic PHP program code. In any case, first of all, a love for PHP is the most basic prerequisite for reading this book.
Chapter 2
PHP has many specifications and syntax. We will not introduce them. Please read books about PHP when you have time. We will focus on the development of PHP after version 4.0. It has added the function of Object-Oriented Programming Language. Although it is not yet completely object-oriented like Java, it is a big improvement compared to the previous version PHP3.0, and it also provides PHP program developers with Provides more powerful and convenient development capabilities.
PHP version 5.0 and above has been transformed into a completely object-oriented version, providing more complete object-oriented functions. The PEAR library we introduced fully supports PHP 4.3.0 and above. Please rest assured on this point.
For the sake of most people, and also taking into account the downward compatibility of PHP, the content and examples of this book can be executed in the environment of PHP version 4.3.0 or above, because PEAR fully supports PHP 4.3.0 or above. Version, in environments smaller than PHP 4.3.0, most of them are supported by PEAR, but only a small part are not supported.
What is object-oriented in PHP? Object-oriented syntax is rarely seen in traditional PHP programs, but if you understand the programming language JAVA, you should be able to know what is object-oriented. I will explain it in Appendix B There is a brief explanation of object orientation.
In addition, I want to emphasize that in addition to basic syntax, PHP has more than a thousand built-in functions, spanning hundreds of categories, including text functions, numeric functions, and database classes. , graphics, etc., the functions are quite powerful and astonishing. But for program developers, how to make good use of these functions and write effective and easy-to-manage code in conjunction with PHP syntax will be a big challenge.
Today, PEAR, a crystallization of wisdom produced by many of the world's top PHP experts, allows us to write code easily and efficiently, and overcome many writing difficulties. Taking the martial arts world as an example, writing PHP with PEAR is like hundreds of martial arts masters pouring their skills into program developers. Program developers do not need to start from the most basic practice of Zha Ma Bu. By borrowing their skills, they can easily double their skills and double their efficiency.
After talking for a long time, what exactly is PEAR? The author will introduce it in the next unit.
The word PEAR simply means "pear" literally.
A pear. The official website address is http://pear.php.net
An example can be seen in the logo of PEAR official website.
PEAR started on November 2l, 1999. There is a Mr. Stig S. Bakken. This man initiated a project and called on many PHP experts to join in. He first established some basic rules of programming for this project and hoped that PHP developers would join this project. All PHP code written during the project needs to comply with these rules.
The name of this project is PEAR, which is actually the abbreviation of PHP Extension and Application Repository, which means "PHP continuation and application library". To put it simply, PEAR is actually a PHP program library.
The basis of the program code created by Mr. Bakken is as follows. I will only briefly explain it and will not go into the details:
◆ If the developer wants to provide a PHP program library, the structure of the program must be object-based.
◆ The indentation of the program must follow a certain format
◆ The naming of categories in the program must also follow a certain format
◆ The comments of the program code must follow a certain format
◆ The file naming must follow a certain format Format
◆ After the program library is written, it must be packaged into a package, and the packaging and installation methods of the package also have a certain format.
This project welcomes all PHP developers to co-develop PHP libraries, as long as they are willing to abide by the above basic coding rules. Mr. Bakken also organized a team to serve as the control body of this project, responsible for reviewing the libraries provided by all developers participating in the project. If they pass the review, the libraries will be classified and placed on the official website for other developers to download and use. , In addition, this group is also responsible for maintaining and updating the official website.
Some people may be curious, why would someone be willing to write a program library for free for others to use? This is the spirit of the open source world - mutual assistance and sharing. Just imagine, if each developer develops a program library for other people to use in the part he specializes in, and also enjoys the program libraries released by other developers, is it possible to achieve the purpose of mutual assistance and allow every developer to share everything? The results of developers. You don't have to write all the code yourself.
What’s even better is that these libraries are free and open to people all over the world. Not only developers involved in library development can enjoy them (this approach is the same as Java or Perl providing function libraries) .
Therefore, all PHP developers around the world can save a lot of time in writing code, and because developers who use the library can report deficiencies in the library to the original developer, the original developer will continue to make corrections. These libraries make the code relatively safe. In addition, library developers will also add new functions to the library as the times evolve, so that library users can keep pace with the times.
For details, please refer to the description of the purpose of developing PEAR mentioned in PEAR's official website: http://pear.php.net/manual/en/about-pear.php. I suggest you read it slowly when you have time. If you continue to read this article, you will slowly understand the magic of PEAR.
I have an appendix at the end of the article: an introduction to the PEAR official website. You can refer to it first.
If you don’t quite understand what I mean by “program library”, it doesn’t matter. I will explain it through practical examples in the next section.
2-3 Introduction to all packages
▓ Library VS Suite
Before introducing all packages, we must clarify two terms to avoid confusion among readers. One is "library" and the other is "package" .
Suppose I am a PHP developer and develop a "library" that can handle all database access, and want to add this library to PEAR to share it with the world. After I finished writing the database management library, I had to compress the library into a "file" file according to PEAR's regulations and put it on the PEAR official website for everyone to download. This package can be installed or managed using PEAR's package management instructions, which is very convenient for PEAR users. Regarding the package management instructions, I will give a complete explanation in the next chapter. After the above explanation, I believe readers should understand What is a "program library" and what is a "package"? In fact, to put it simply, the program library is packaged into a suite.
▓ Kits and Categories
PEAR has released more than 300 kits so far, divided into 35 categories, and the number is constantly increasing. You can imagine what a uniting force the world's top PHP developers can bring together. The picture below shows the classification of kits on the official website:
PEAR has released too many kits. For the convenience of management, it classifies the kits according to their functions. Kits of the same type are all related in effect. However, the functions provided are not the same. Next, I will give a brief introduction to all categories in a table to let everyone know more about what functions the PEAR suite can provide.
There are so many categories, and each category will develop different packages according to different functions. Take the first authentication category as an example. If you go to the official website, you will find that there are currently 8 packages in this category
These 8 packages are all related to identity authentication. If you want your webpage to have Identity authentication function, for example, allows web browsers to register, enter account and password, identify identity, grant permissions, etc. These kits will be needed.
I would like to add one more thing: some packages have matured and developers will call them stable versions. However, if the library is still under development but is already available, the developers will release it. Usually It will be called an alpha or beta version. The official website will also record the names of the developers and maintainers of the package. Sometimes a package is developed by many people.
Information on these packages can be found on the PEAR official website. I will introduce how to use the PEAR official website in the appendix "Introduction to the PEAR official website".
2-4 Simple examples of using the PEAR library
In this section, I use a simple example to illustrate the use of the library.
Suppose you want to create a web page and want to know the time it takes to execute the web page, you can borrow a library called Benchmark released by PEAR. This library was developed to calculate the execution time of web pages.
First, install the Benchmark library package (please refer to the next chapter for the installation method). Then we can start writing web pages. For example: we created a web page named show.php.
For the convenience of explanation, I add a line number in front of each line of the program. In fact, you don’t need to add a line number when writing the program.
The code in lines 1 and 2 first refers to the Timer.php web page in the Benchmark package. This web page contains a library that calculates the execution time of the program page. include_once can put an external web page into this page. I call this action "reference". This example is equivalent to executing two web pages, Timer.php and show.php.
In line 3, use the new keyword to create a Benchmark_Timer object and put the object into a variable named $timer. Then use the start() method of the $timer object to start the clock, and then add my own code, in this case a simple line of code.
The next step is to end the clock, calculate the time it takes to execute the web page and display it. Line 8 first uses the stop() method of the $timer object to stop the clock, and uses the timeElapsed() method of the $timer object on line 9 to calculate the time difference from starting to stopping the clock, and puts the calculation result into the $totaltime variable.
Finally, use the echo function to display the time difference on the web page.
It can be clearly seen from the results that the show.php web page took a total of 0.00013613700866699 seconds from the beginning to the completion of execution. In the future, you can observe this number to judge the system performance, or as a reference for code improvement. How about, is the calculation of execution time convenient, fast and magical!
If you need to have the function of calculating the execution time of a web page on other web pages, just use the above method to apply it to other PHP web pages.
In fact, the use of other packages is similar to the use of this package. The difference is only in the categories, methods and parameters used by each package.
Although the package is easy to use, I believe readers still have many doubts? For example:
1. Where does the Benchmark library package come from? What preparatory work is required to use it?
2. Me How do you know how to use the Benchmark package?
The first question involves how to install the PEAR package, as well as the management, upgrade, and deletion of the package after installation. I will introduce it in Chapter 3. For the second question, the author will give a detailed explanation on how to use the kit and how to obtain the information about the kit in Chapter 4.
2-5 Why use PEAR
Why use PEAR? Based on my own experience, I summarized the following points:
1. Save time and effort: many codes are written in the program library. Developers only need to reference the PHP file containing the library to have the functions provided by the library, saving the time and energy of writing their own.
2. Security: Since these library packages make their source code public, if users find bugs, they can immediately report them to the developers of the packages, and the developers will usually correct them immediately to minimize library errors. .
3. Easy to maintain: The self-written code is separated from the code of the PEAR library. If the PEAR package releases a new version because new functions are developed or bugs are corrected, you only need to update the package. It will not work at all. Move to self-written code.
4. Powerful functions: PEAR suite provides many functions, in addition to common functions such as: database linking, configuration file processing, identity authentication, and form processing. In addition, more advanced functions, such as online post office, network management, dynamic generation of graphics, RSS, Wiki, or BBCode analysis like phpBB, etc. can be easily implemented.
5. Separate program development and layout design of web pages: PEAR provides many sample program libraries to separate program development and layout design, making it easier for program developers and web designers to work together.
6. It is an excellent learning example for those who learn PHP: the annotations of the program library are very detailed, and the writing of the program code has certain rules and formats, and is completely materialized, making it easy for developers to understand and study. Some packages also come with teaching files and sample files. The most important thing is: you can get them without spending half a dime. We should thank the authors of the packages for their selfless contributions to us.
2-6 Why establish the PEAR community
What is the reason that prompted PHP developers around the world to unite and jointly develop the treasure house of PEAR? I think there are several reasons:
1. Suffer strong competition from other competitors: This point has been explained in Chapter 1.
2. Materialization of PHP: After adding material-oriented functions from PHP4, PHP5 is further materialized. In addition to competing with ASP.NET and JSP, it also facilitates the modularization of programs and enables the development of program libraries. Only with materialized functions can the program libraries be separated from the program codes written by developers themselves.
3. Common needs of developers: If every time you develop a PHP program, you have to do it from scratch. Although you can exercise your programming skills, it is inefficient and not very economical. If some commonly used functions can be handled in the form of a program library, it will solve many developers' difficulties. Based on this need that all developers have in common, let these PHP developers work together to develop the program library to help themselves and others.
2-7 Competitors, Advantages and Disadvantages of PHP
PHP was developed in 1994. After four major rewrites, it has become the widely used and mature product we see today. As of April 2005, PHP has been used on more than 19 million websites around the world, and this number continues to grow rapidly.
Compared with their competitors, PHP has the following advantages:
High performance: I am not selling melons, haha. .., I have browsed many Asp.NET and Jsp web pages, but the performance is really not comparable to PHP. (Users of Asp.NET and Jsp probably won’t read this article).
● Can be connected to many different database systems: other competitors are also available, but not as fast as PHP.
● Easy to learn and use: The writing method of PHP code is very intuitive and easy to learn.
● Support object orientation: Jsp is strong in this aspect, but PHP5 has caught up.
● Low cost: no money at all.
● There are many built-in function libraries: I remember that I used Asp before, and the built-in function libraries were very few.
● Open source code: The security will be higher than if it is not open source.
● PEAR library: This is the latest powerful weapon, used to compete with powerful enemies.
● Portability: PHP can be used in various Unix operating systems, such as Linux, FreeBSD, commercial version of Unix or Windows operating systems.
Asp.NET and Jsp certainly have their unique advantages? For example, Jsp carries the powerful functions of JAVA, and it can present some effects that PHP cannot currently achieve. Therefore, Jsp has also attracted many users, but the author is really not satisfied with the current performance of Jsp. On the contrary, I am extremely satisfied with the speed of PHP.
Which program to use is up to you to make a clear decision. However, if you choose to read the article, I believe that we should all be in the same camp, PHP enthusiasts.
All in all, many PHP experts have built a strong support network together. If you are a PHP program developer, but you don’t appreciate the kindness of these experts, wouldn’t it be a pity?