Home > Backend Development > PHP Tutorial > Eight things to note about PHP code architecture_php tips

Eight things to note about PHP code architecture_php tips

WBOY
Release: 2016-05-16 19:59:25
Original
1264 people have browsed it

This article summarizes eight points to note about PHP code architecture. Share it with everyone for your reference, the details are as follows:

I have been writing code for more than 6 years. Look at the previous architecture and look at the current architecture. All have some shortcomings. No matter what, it's always improving. To be honest, I don’t really like using things like frameworks. Basically, all frameworks are similar, not much different. To use them, you have to understand the ideas, directory structure, file calling, file naming, code specifications, etc. It’s quite a waste. of time. The architecture can meet a large part of the requirements. The more people it meets, the worse the performance. This is one of the main reasons why I don't like to use architecture. Let’s talk about some thoughts on code architecture.

1. Mainly object-oriented, supplemented by process-oriented

php4 is process-oriented, and php5 and later supports object-oriented.

1. Object-oriented programming ideas, for code management, more reasonable planning, better performance, etc.
2. For process code management, the planning is relatively simple, the calling method is more direct and clear, and the efficiency is relatively poor.
Let’s give an example:
Object-oriented is like a software park. If you want to find the location of a certain company, you need to know which building, which floor, and which room it is.
Process-oriented is like a residential area. If you want to find a certain house, you need to know which road and what number.
If the high-rise buildings in the software park are to be turned into residential buildings, more land will be occupied. If the campus is too large, it will not be cheap to find. It would be much more convenient to have a separate consultation room. Therefore, I personally feel that a good architecture must be mainly object-oriented and supplemented by process-oriented.

2. Directory structure planning

This is very important when structuring the code. It is like a building in a software park. It cannot be built casually, it must be built regularly. Where are the entry files placed, where are the function modules placed, and where are the template files placed. In a word, arrange the configuration files, JS files, php files, module files, CSS files, image files, etc.

3. Single entrance or multiple entrances

The PHP frameworks and CMS that I know so far are basically single-entry. Single entrance or multiple entrances, each has its own advantages. One thing to mention is that a single entrance is definitely better than multiple entrances, regarding URL rewriting.

4. Object-oriented hierarchical design

Under normal circumstances, basically three levels are enough, the bottom class, the basic bottom module class, and the application class of the combined module. If it is a relatively large website, there will be more levels.

5. The database class must be planned well

This is very important. The ideal way is to use one class for all sql execution. Why is this? This is a long-term plan. In the future, if you build a mysql cluster or use nosql such as memcache, it will be easy to convert. If sql is available everywhere, it will be depressing.

6. Class sharing under multiple second-level domain names

What does it mean? For example, a website has BBS, portal, Weibo. Normal thinking (except discuz, so the second-level domain name points to the same directory) corresponds to three directories, that is, there are three documentroots. The best shared class is Exists independently of the web directory. This way classes can be shared and code redundancy can be reduced.

7. Using template plug-ins

I don’t think it’s good to mix php code and html code, especially if the website is revised, it will crash you. There are many templates that separate php code and html, such as: smarty

8. js code planning

Smarty can separate php and html, jquery and other similar js plug-ins can separate js and html. JS is also a development language, and plug-ins based on JS can also be understood as a language, both object-oriented and process-oriented. If you don't plan it well, the html page will be messy. If you look at the source code, you will find that it is half html and half JS.

Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction Tutorial on PHP Basic Syntax", "Introduction Tutorial on PHP Object-Oriented Programming" and " Summary of excellent PHP development framework

I hope this article will be helpful to everyone in PHP programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template