Home  >  Article  >  Backend Development  >  Do you know these three difficult PHP knowledge points?

Do you know these three difficult PHP knowledge points?

藏色散人
藏色散人forward
2019-12-11 11:13:582895browse

Although php is easy to get started, there is still some way to go if you want to go deeper. Today we will talk about some knowledge points of php that are difficult to understand.

● use

php has introduced a new feature called namespace since 5.3.0, which allows us to define the same classes and handle them as third-party libraries There is a good solution for classes with the same name. A namespace can include classes, functions, interfaces and constants. After having a namespace, when you need to use the namespace elsewhere, you need to use use to introduce the namespace or Define an alias for the namespace and then use it. This is the first use case of use. It is important to note that the use here does not load the corresponding file.

When using PHP, we found that many functions have a callback function parameter. We can use anonymous functions (also called closures to achieve this). At the same time, we know that they cannot be used directly in PHP functions. External variables, what should I do if I want to use external variables inside the closure? Refer to the example below, which can be done using use. This is the second usage of use.

As for the third usage of use, please see Traits below.

Do you know these three difficult PHP knowledge points?

● Traits

We know that php is not a multiple inheritance language like C, it is a single inheritance language , what if sometimes we need to inherit multiple classes? Traits come into play at this time. We only need to use use to declare multiple traits in the class, so that the method with the same name in the current class overrides the trait, and the trait overrides the method with the same name in the base class, and the attributes are the same.

Do you know these three difficult PHP knowledge points?

● The difference between errors and exceptions

Whether an error occurs or some exception is not caught by us, our program will appear problem, and whether it is an error or an uncaught exception, the performance of both is the same, which leads to our understanding of errors and exceptions. In fact, exceptions are theoretically no problem with the program, but when running, due to certain Special circumstances terminate the running of the program, such as the operation of linking to the database. If everything is fine, then we should be able to link to the database normally, but when the number of available connections to the database is gone, or the database cannot be linked, then at this time, if If this exception is not caught, something will definitely go wrong.

Do you know these three difficult PHP knowledge points?

The above is the detailed content of Do you know these three difficult PHP knowledge points?. For more information, please follow other related articles on the PHP Chinese website!

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