Home  >  Article  >  PHP Framework  >  2018PHP interview questions: ThinkPHP

2018PHP interview questions: ThinkPHP

coldplay.xixi
coldplay.xixiforward
2020-08-10 17:18:564010browse

2018PHP interview questions: ThinkPHP

1. Common PHP frameworks

Answer: thinkPHP, yii, ZendFramework, CakePhp, sy

Related topic recommendations: 2020 thinkphp interview questions and answers (complete collection)

2. How to understand the single entry file in TP?

Answer: ThinkPHP uses a single entry mode for project deployment and access. No matter what function is completed, a project has a unified (but not necessarily the only) entry. It should be said that all projects start from the entry file, and the entry files of all projects are similar. The entry file mainly includes:

Define the framework path, project path and project name (optional)

Define related constants for debugging mode and running mode (optional)

Load the framework entry file (required)

3. The MVC layering in ThinkPHP is What? (Understanding)

Answer: MVC is a method of separating the logical layer and presentation layer of an application. ThinkPHP is also based on the MVC design pattern. MVC is just an abstract concept and has no particularly clear regulations. The MVC layering in ThinkPHP is roughly reflected in:

Model (M): The definition of the model is completed by the Model class.

Controller (C): Application controller (core controller App class) and Action controller both assume the role of controller. Action controller completes business process control, while application controller is responsible for scheduling control.

View (V): It is composed of View class and template file. The template is 100% separated and can be previewed and produced independently.

But in fact, ThinkPHP does not rely on M or V, which means it can work without a model or view. It doesn’t even rely on C. This is because ThinkPHP also has a master controller on top of Action, the App controller, which is responsible for the overall scheduling of the application. In the absence of C, view V must exist, otherwise it is no longer a complete application.

In short, ThinkPHP’s MVC model only provides a means of agile development, rather than sticking to MVC itself.

4. How to optimize SQL? (Students can understand the following explanation, and then just state the general meaning according to their own understanding)

Answer: (1) Choose the correct storage engine

Taking MySQL as an example, it includes two storage engines, MyISAM and InnoDB. Each engine has advantages and disadvantages.

MyISAM is suitable for applications that require a large number of queries, but it is not very good for a large number of write operations. Even if you just need to update a field, the entire table will be locked, and other processes, even the reading process, cannot operate until the reading operation is completed. In addition, MyISAM is extremely fast for calculations such as SELECT COUNT(*).

The trend of InnoDB will be a very complex storage engine. For some small applications, it will be slower than MyISAM. But it supports "row locking", so it will be better when there are many write operations. Moreover, it also supports more advanced applications, such as transactions.

​ (2) Optimize the data type of the field

​ Remember a principle, the smaller the column, the faster it will be. If a table only has a few columns (such as a dictionary table, configuration table), then we have no reason to use INT as the primary key. It will be more economical to use MEDIUMINT, SMALLINT or a smaller TINYINT. If you don't need to keep track of time, it's much better to use DATE than DATETIME. Of course, you also need to leave enough room for expansion.

 (3) Add an index for the search field

The index does not necessarily mean the primary key or the only field. If there is a field in your table that you will always use for searching, then it is best to index it. Unless the field you want to search is a large text field, then you should create a full-text index.

(4) Avoid using Select *. The more data is read from the database, the slower the query will become. Moreover, if your database server and WEB server are two independent servers, this will also increase the load of network transmission. Even if you want to query all fields in the data table, try not to use the * wildcard character. Making good use of the built-in field exclusion definitions may bring more convenience.

(5) Use ENUM instead of VARCHAR

The ENUM type is very fast and compact. In fact, it holds a TINYINT, but it appears as a string. In this way, it becomes quite perfect to use this field to make some choice lists. For example, if the values ​​of fields such as gender, ethnicity, department, and status are limited and fixed, you should use ENUM instead of VARCHAR.

(6) Use NOT NULL whenever possible

Unless you have a very special reason to use NULL values, you should always keep your fields NOT NULL. NULL actually requires extra space, and your program will be more complex when you perform comparisons. Of course, this does not mean that you cannot use NULL. The reality is very complicated, and there will still be situations where you need to use NULL values.

(7) Fixed-length tables will be faster

If all fields in the table are "fixed-length", the entire table will be considered "static" or "fixed-length" ". For example, there are no fields of the following types in the table: VARCHAR, TEXT, BLOB. As long as you include one of these fields, the table is no longer a "fixed-length static table" and the MySQL engine will process it in another way.

Fixed-length tables will improve performance because MySQL will search faster. Because these fixed lengths make it easy to calculate the offset of the next data, reading will naturally be faster. . And if the field is not of fixed length, then every time you want to find the next one, the program needs to find the primary key. Also, fixed-length tables are easier to cache and rebuild. However, the only side effect is that fixed-length fields waste some space, because fixed-length fields require so much space regardless of whether you use them or not.

5. How to understand the behavior in ThinkPHP 3.0 architecture three (core behavior driver)?

Answer: Core Behavior Driven

The official abbreviation of TP is: CBD

Core: It is the core code of the framework, an indispensable thing, the TP itself It is a framework developed based on MVC ideas.

Behavior (Behavior): Behavior plays a decisive role in the architecture of the new version of ThinkPHP. On top of the system core, there are many tag extension bits, and each tag position can execute its own independent behavior in turn. This is how behavioral extensions were born, and many system functions are also completed through built-in behavioral extensions. All behavioral extensions are replaceable and additive, thus forming the basis for the assembly of the underlying framework.

Driver: database driver, cache driver, tag library driver and template engine driver, as well as external class extensions.

Frame, that is, framework. In fact, it is a semi-finished product of a certain application, a set of components for you to choose and use to complete your own system. To put it simply, you use the stage set up by others and you perform. Moreover, frameworks are generally mature, continuously upgraded software.

6. What is the conventional configuration?

Answer: Conventional configuration Previous page Next page Convention is more important than configuration. This is an important idea that the system follows. The system has a built-in convention configuration file (Conf\convention.php located under the system directory) , the common parameters are configured by default according to most uses. Therefore, for the configuration file of the application project, you often only need to configure different or new configuration parameters from the conventional configuration. If you completely adopt the default configuration, you do not even need to define any configuration file.

Conventional configuration files will be automatically loaded by the system and do not need to be loaded in the project.

7. What is SQL injection? (Understanding)

Answer: SQL injection attack is one of the common means used by hackers to attack databases. Some programmers do not judge the legality of user input data when writing code. The injector can enter a database query code in the form and submit it. The program will piece together the submitted information to generate a complete SQL statement, and the server will be deceived. Execute the malicious SQL command. The injector successfully obtains some sensitive data based on the results returned by the program, and even controls the entire server. This is SQL injection.

8. How does ThinkPHP prevent SQL injection? (Understanding)

Answer: (1) Try to use arrays for query conditions, which is a safer way;

(2) If you have to use string query conditions, Use the preprocessing mechanism;

(3) Turn on data field type verification, and you can force conversion of numerical data types; (Field type verification has been mandatory since version 3.1)

(4) Use automatic verification and automatic completion mechanisms to customize application-specific filtering;

(5) Use field type checking, automatic verification, and automatic completion mechanisms to avoid the entry of malicious data.

9. How to enable debugging mode? What are the benefits of debug mode?

Answer: Turning on debugging mode is very simple. You only need to add a line of constant definition code to the entry file:

After completing the development phase and deploying it to the production environment, you only need to delete the debugging mode. Define the code to switch to deployment mode. After debugging mode is turned on, the system will first load the system's default debugging configuration file, and then load the project's debugging configuration file. The advantages of debugging mode are: Turn on logging, and any error information and debugging information will be recorded in detail to facilitate debugging; Turn off template caching , template modifications can take effect immediately; record SQL logs to facilitate SQL analysis; turn off field caching, data table field modifications are not affected by caching; strictly check file case (even on Windows platforms), helping you discover Linux deployment problems in advance; can be convenient Used for different stages of the development process, including development, testing and demonstration, etc., independent project configuration files can be configured for different application modes.

10. What configuration modes are supported in TP? priority?

Answer: ThinkPHP has created its own unique hierarchical configuration mode in project configuration. Its configuration level is reflected in: Conventional configuration->Project configuration->Debug configuration->Group configuration ->Extended configuration->Dynamic configuration

The above is the loading order of configuration files. Because the subsequent configuration will overwrite the previous configuration with the same name (without taking effect), the priority order is from right to left. .

11. What are the URL patterns in TP? Which is the default?

Answer: ThinkPHP supports four URL modes, which can be defined by setting the URL_MODEL parameter, including normal mode, PATHINFO, REWRITE and compatibility mode.

The default mode is: PATHINFO mode, set URL_MODEL to 1

12. What are the system variables in TP? How to get system variables?

Answer: How to get system variables:

Just call the following method in Action:

$this->Method name ("Variable name" ,["Filter method"],["Default value"])

13. What is the difference between D function and M function in ThinkPHP framework?

Answer: The M method does not require the user to define a model class for each data table when instantiating the model. The D method can automatically detect the model class. If a custom model class exists, instantiate the custom model class. , if it does not exist, the M method will be automatically called to instantiate the Model base class. At the same time, models that have been instantiated will not be instantiated repeatedly (single case mode).

Related learning recommendations:thinkphp

The above is the detailed content of 2018PHP interview questions: ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:wutongketang. If there is any infringement, please contact admin@php.cn delete