Home > PHP Framework > ThinkPHP > body text

Does thinkphp3 have a facade class?

WBOY
Release: 2022-04-07 16:45:39
Original
2119 people have browsed it

thinkphp3 has a facade class. Facade provides a static calling interface for classes in the container, with better testability and scalability. You can define a facade class for any non-static class library. The syntax is "class Test{public function Class(){return path;}}".

Does thinkphp3 have a facade class?

The operating environment of this article: Windows 10 system, ThinkPHP3 version, Dell G3 computer.

Does thinkphp3 have a facade class?

The facade provides a static calling interface for classes in the container. Compared with traditional static method calls, it brings better testability and expansion. You can define a facade class for any non-static class library

Why use facade

1. Static methods and The difference between non-static methods (different calling objects and reference variables)

Static method: It is a method modified with the static keyword, also called a class method. It belongs to the class, not to the object, and is instantiated Objects can call static methods through class name and method name before. (Static attributes and static methods belong to classes and can be called directly through the class name).

A. In a static method, you can call a static method.

B. In a static method, non-static methods cannot be called.

C. In a static method, you can reference class variables (that is, variables modified by static).

D. In a static method, member variables cannot be referenced (that is, variables without static modification).

E. In static methods, the super and this keywords cannot be used

Non-static method: It is an ordinary method that does not contain the static keyword modification, also known as instance method and member method. Belongs to objects, not to classes. (Member attributes and member methods belong to the object, and the object must be created through the new keyword and then called through the object).

A. In ordinary methods, ordinary methods can be called.

B. In ordinary methods, you can call static methods

C. In ordinary methods, you can reference class variables and member variables

D. In ordinary methods, You can use the super and this keywords

2. The difference between static methods and non-static methods (different calling methods)

Static methods can be called directly, and the class name call and Object call. (Class name. Method name / Object name. Method name)

But non-static methods can only be called through objects. (Object name. Method name)

3. The difference between static methods and non-static methods (different life cycles)

The life cycle of a static method is the same as that of the corresponding class Long, static methods and static variables are allocated and loaded into memory as the class is defined. Static properties and methods will not be destroyed until the thread ends. (That is, static methods belong to classes)

The life cycle of non-static methods is as long as the instantiated object of the class. Only when the class instantiates an object, the non-static method will be created, and when this object When destroyed, non-static methods are also destroyed immediately. (That is, non-static methods belong to objects)

Summary:

Class methods can be called directly through the class name, and instance methods must first instantiate the class and then initialize the object. Then the instance object of the class can be called

How to use

1. Create a folder facade

# where the facade class needs to be placed. ##2. Write your own public class

3. Create the facade class code in the facade as follows

use think/Facade;
class TestFacade extends Facade{
protected static function getFacadeClass(){
return '写好的目标公共类绝对路径';
}
}
Copy after login

4. When calling this public class, you can directly use the facade class to replace the public class, so Non-static methods of public classes can also be called statically

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Does thinkphp3 have a facade class?. For more information, please follow other related articles on the PHP Chinese website!

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