10PrinciplesofthePHPMasters in the WEB development world, PHP is one of the most popular languages. From PHP, you can easily find the scripts you need. Unfortunately, few people will write a PHP program using best practices. Here, we will introduce you to the 10 best practices of PHP, of course, each of which is
10 Principles of the PHP Masters in the WEB development world, PHP is one of the most popular languages. From PHP, you can easily find the scripts you need, unfortunately, few people write a PHP program using the best practices. Here, we will introduce you to the 10 best practices of PHP, of course, each of which is
10 Principles of the PHP Masters
In the WEB development world, PHP is one of the most popular languages. From PHP, you can easily find the scripts you need. Unfortunately, few people will use "best practices" to write a PHP program. Here, we will introduce you to the 10 best practices of PHP, of course, each of which has been proven by masters.
1. Use PHP-Rasmus Lerdorf whenever appropriate
No one knows where PHP is used more rationally than Rasmus Lerdorf, the creator of PHP. he released the PHP language in 1995. Since then, PHP has been like an original fire, it burned the entire development camp and changed the Internet world. But Rasmus didn't create PHP for this. PHP was born to solve the actual problems of WEB developers.
Like many open-source projects, PHP has become popular, and the motives of pop-ups are not explained in the normal philosophy, and they are even somewhat arrogant. It can be used as a case, a case that is caused by the need for tools to solve various Web problems. Therefore, when PHP emerged, all such tool requirements were focused on PHP.
However, you cannot expect PHP to solve all problems. Lerdorf is the first person to admit that PHP is just a tool, and PHP has a lot of power.
Select an appropriate tool based on your work. I ran a lot of companies to persuade them to deploy and use PHP, but this does not mean that PHP applies to all problems. It is just a front-end step language that can solve most problems.
As a web developer, It is unscientific to try to use PHP to solve all problems, and it will also waste your time. Do not hesitate to try other languages when PHP is not easy to use.
2. Use multi-Table storage to improve scalability-Matt Mullenweg
No one is willing to question Matt Mullenweg's PHP authority. He developed the most popular blog system on the planet (powered by a powerful Community): WordPress. after creating Wordpress, Matt and his team started the WordPress.com platform, a free blog site based on WordPress MU. Currently, Wordpress.com already has approximately 4 million users. These users provide more than 140,000 logs every day.
If someone knows how to scale a website, this person must be Matt Mullenweg. In 2006, Matt made a forward-looking improvement on the Wordpress data structure and explained why Wordpress MU used an independent MYSQL table for each blog, instead of stuffing all the blog data into a huge table.
We tested this method, but found that it would be too costly to scale it out. If you use an overall data structure, you will face server hardware problems in the face of high traffic volumes. In MU. Users are distributed to independent tables and can be easily organized. For example, WordPress.com stores user data in 4096 databases. These databases can distribute large-scale data access and distribute traffic and pressure.
Data Table migration allows code (blog) to run faster and the system to be more scalable. Powered by powerful cache policies and flexible database application policies, Matt showed people that the most popular Facebook and Wordpress.com can run stably in PHP and handle amazing traffic volumes.
3. Never trust user Dave Child
Dave Child is the core character of the Added Bytes (previously ilovejackdaniels.com) website, which is famous for its outstanding cheat sheets for your programming ages. Dave serves many British companies and has established considerable authority in the programming world.
Dave provides PHP developers with a lot of thoughtful suggestions and summarizes the writing secure code in PHP: do not trust your users, they may even hurt you.
There is a basic principle of web development. I think it is not enough to repeat it many times, that is: do not trust your users, at the same time, assume that every data unit on your website is malicious code collected from users. Most of the time, you must use JAVAscript to check the content submitted in the form on the client. If you are used to this, this is a good habit. If security is important to you, this is the most important principle to be learned.
Dave is currently working on organizing examples for its Writing Secure PHP series of books. At the end of the book, he said:
Finally, be paranoid. Unless you think that your site will never be attacked, you will face up to all problems. When the problem really happens, your situation will become very bad. You need to think of every user as a hacker who will bring an Attack and Defense station, do everything possible to protect the security of the site, and think about solutions to the corresponding problems.
4. Use PHP cache more-Ben Balbo
Ben Balbo developed Site Point, a website that provides guidance for developers and designers. He is a member of the PHP development and open source club in Melbourne. Therefore, he has a certain understanding of PHP and has some ideas and experience on PHP caching.
If you have a site with a large volume of traffic but not frequently updated (such as a blog, based on a CMS), it may need some modifications that will not take too much time, however, it has made outstanding contributions to performance. If you want to set up a caching mechanism for a complex or frequently updated site, the process may be tortuous, but the benefits are also obvious.
There are many PHP cache technologies. Ben recommends the following for us:
- Cache function running result
- Set expiration time
- Cache files downloaded by IE
- Template Cache Technology
- Cache_Lite
As PHP is a dynamic language feature, the cache mechanism is very important for sites with low update frequency.
5. Use IDE, Templates, and Snippets to accelerate PHP development-Chad Kieffer
When Chad Kieffer draws out of the UI design and database optimization work, he will share many technical experience on his blog 2 tablespoons. Due to the comprehensive development of Chad in many aspects, he often finds problems that other programmers cannot discover and forms relevant experience, especially his website development methods. He participates in all aspects of website development, so his suggestions are very useful for improving the overall situation of website development.
Chad believes that using an IDE such as Eclipse PDT (Eclipse's PHP developage package) and using some template technologies and open-source projects can effectively improve PHP development speed.
The compact plan and long to do lists and deadlines make developers suffer. However, some functions, such as Eclipse Templates, can effectively reduce the coding time and the probability of errors.
Generally, any project can be automated. The higher the degree of automation, the shorter the time you need to complete the project. Taking the time to develop highly frequently used frameworks and templates will save you more time in the future. At the same time, using an IDE like Eclipse and the PDT package, you will find that the efficiency is significantly improved. the IDE can be automatically closed, supplemented with semicolons, and can be debugged locally.
6. Use the PHP filter function-Joey Sochacki
Maybe Joey Sochacki is not as famous as Matt Mullenweg, but he is also an experienced developer and shares a lot of technical experience through his blog Devolio
Joey found that there are many places to filter in the php code writing process, but there is not much coder concerned with php's built-in filter function.
Data filtering is something we often need to do, but it is unknown that PHP built-in filtering functions are rich in many functions. Using PHP built-in functions similar to filter _ *, we can process almost all filtering tasks, including data type verification, URL/email, and IP address verification, and special character processing.
Filtering is a complicated task, but I believe joey's discovery will inspire you a lot and make you realize the powerful filtering functions of PHP.
7. Use the PHP framework-Josh Sharp
There has been a lot of controversy over whether Zend, CakePHP, Code Igniter, or other PHP frameworks should be used, but in the minds of web developers, they have their own standards.
Josh Sharp created a website that provides the bread and butter services, so he has some experience developing websites using the PHP framework. He believes that using a PHP framework for project development can effectively save time and reduce the chance of errors. Why? He thinks PHP is really easy to use.
The ease of use of PHP is sometimes flawed, because not strict syntax often leads to the birth of many error codes. However, if you use a PHP framework, the chances of errors will be greatly reduced.
The PHP framework makes your code structure more standard and saves a lot of time.
8. Do not use the PHP framework-Rasmus Lerdorf
Unlike Josh, Rasmus Lerdorf, the originator of PHP, thinks it is best not to use the PHP framework. Why? Because PHP performance is better than framework-based. Rasmus used the "Hello World" example in Drupalcon 2008 to compare the performance of some frameworks between PHP and simple PHP. The results show that the performance of the Framework PHP is far behind.
9. Use batch processing-Jack D. Herrington
Jack Herrington is no stranger to the PHP world. He has contributed more than 30 proprietary articles to the famous IBM developerWorks and published the book "PHP Hacks", so he is a real expert.
Herrington recommends using batch processing and Cron to replace those programs that can run in the background. Web users are not willing to wait for your processing process online, so some tasks are more suitable for processing in the background.
It is true that in some cases this is a little tricky, but you can clearly see that using Cron, MySQL, PHP object-oriented methods and Pear :: creating a batch processing tool is not complicated.
Jack believes that it is much more cost-effective to use cron, PHP, and MySQL to process some tasks in the background than to start multi-process business logic.
I have tried both methods. I think Cron is very consistent with the "Keep It Simple, Stupid" (KISS) principle, which makes background processing easy. Compared with the multi-process business logic, it has no risk of memory overflow. You can create a simple batch processing script and run it in cron. This script regularly checks whether a task needs to be processed. After processing, it automatically exits, therefore, you don't have to worry about whether the process is shelled or stuck in an endless loop.
10. Enable error reports in a timely manner-David Cummings
David Cummings has a company dedicated to CMS software services and won several awards. He has rich PHP development experience. David once wrote two PHP tips he wished he 'd learned in the beginning, one of which is: Enable error reports in time, which saves a lot of time.
I told people that the most important thing is to enable the PHP Error Report to the maximum extent. Why? Because PHP may hide many minor issues:
- The variable is not predefined.
- Reference unavailable variables in code snippets
- Using undefined constants does not seem like a big deal unless you are writing some class libraries using object-oriented methods. In general, disabling error reports may cause you to spend more time maintaining your code.
The error report can help you easily locate code problems. If the error report level is high enough, minor errors can be detected immediately, saving you the overall debugging time.