


Thinkphp internal function ADSLCFUI shortcut method full analysis_PHP tutorial
ThinkPHP defines shortcut methods for some commonly used operations. These methods have single-letter method names, which are easier to remember. What is very interesting is that the letters of these shortcut methods contain the letters ADSL, so we call them ADSL methods. These shortcut methods A, D, S, L, C, F, U, and I are all in the file /THINKPHP/Common/functions.php. Below I will explain their respective functions and usage.
A() loads Action class
D() loads Model class
S() global cache configuration
L() gets the language definition
C() Get configuration value
F() fast file data reading and saving for simple type data strings, arrays
U() is used to complete the assembly of URL addresses
I() quickly creates an object instance
1.A Quickly create Action objects
$action=new UserAction(); // Equivalent to the following writing: $action=A("User"); Moreover, if the current UserAction class has not been introduced, the A method will be automatically introduced. And it has the support of singleton mode, so the same Action object will not be created repeatedly.
Method A supports cross-project calls, for example:
$action=A("User",'Admin'); //Instantiate the UserAction class of the Admin project
2.D Quickly create model data objects
First define the model class, such as UserModel, and then you can use the D() function to operate on the data. For example:
First create a PHP script named UserModel.class.php under "Your Project"/Lib/Model, with the following content:
class UserModel extends Model{}
Then, without adding any attributes and methods, you can perform the following operations:
$User=D("User"); //Instantiate the User object. User is a data table named "prefix_user" that you created in the database. You can also use $User=new UserModel() instead for instantiation. operations on objects. After instantiation, you can perform a series of operations such as adding, deleting, checking, and modifying data, such as:
$User->find(1); //Find the record with primary key 1
When we are doing user gold coins or points or voting, we need to add 1 to the specified field. At this time, I can write like this
$User->score='(score+1)';$s->save(); This will save us a lot of steps.
If you want to modify a specified field, it can be abbreviated as follows:
D('User')->setField('name','hehe','id=2');
The main differences between D method and M method are:
The M method does not need to create a model class file. The M method does not read the model class, so automatic verification is invalid by default, but it can be achieved through dynamic assignment; while the D method must create a model class, we You can use the following two methods to create a mapping object of a data table.
The first type: $Test=D('Test');
Second type: $Test=new Model('Test');
Although both of them can perform select, insert, delete, and udpate operations on data, they are very different in data verification. Using the first method to instantiate a model will have a data checking function. For example, you can define if If the title is not filled in, it will prompt "Please enter the title" (this is an automatic verification function provided by tp, of course, the verification conditions also need to be defined in the corresponding model);
D method can automatically detect the model class, and it will throw an exception if it does not exist. At the same time, the instantiated model will not be instantiated repeatedly (single case). The default D method can only support calling models under the current project (or application). For example:
$user=new UserModel();
Equivalent to $user=D('user');
If an empty model is instantiated, for example:
$Demo=new Model();
Then it is equivalent to:
$Demo=M();
3.S Quick Operation Caching Method
ThinkPHP abstracts various caching methods into a unified cache class for calling, and ThinkPHP unifies all caching mechanisms into an S method for operation, so you do not need to pay attention to specific caching details when using different caching methods. Such as:
S('data',$Data); //Use data identifier to cache $Data data
S('data',$Data,3600); //Cache $Data data for 3600 seconds
$Data=S('data'); //Get cached data
S('name',null); // Delete cache identifier name
4.L Quickly operate language variables
The L method provides multi-language support and can quickly set and obtain language definitions.
L('USER_INFO','User information'); //Set the language variable named USER_INFO
L('USER_INFO'); //Get the language variable value of USER_INFO
//Batch assignment
$array['USER_INFO']='User information';$array['ERROR_INFO']='Error information';
L($array);
5.C Quickly operate configuration variables, the usage is C ("Fill in the subscript of the array in the configuration file here")
C('USER_AUTH_ON',true); //Set the configuration parameter named USER_AUTH_ON
C('USER_AUTH_ON'); //Get the variable value of USER_AUTH_ON
Like L, C also supports batch assignment
Note: Configuration parameters are not case sensitive
In addition, starting from version 1.5, the C method also supports the operation of two-dimensional arrays, such as:
C('USER.USER_TYPE',1);
C('USER.USER_AUTH_ON');
6. F file data saving method
The F method is mainly used for writing, changing and deleting the file data of the project. Its working mechanism is similar to the S method. The difference is that the purpose is different and the directory where the data is saved is also different, and the caching method cannot be specified because the default It is to save data in file form. The F method uses the var_export method, so it can only support simple data types and does not support object caching.
7. U is used to complete the assembly of URL addresses. The characteristic is that it can automatically generate the corresponding URL address according to the current URL mode and settings
The format of this function is: U ('address', 'parameters', 'pseudo-static', 'whether to jump', 'display domain name'); the advantage of using the U method in the template instead of fixing the hard-coded URL address The thing is, once your environment changes or parameter settings change, you don't need to change any code in the template. The calling format in the template needs to be in the form of {:U('address', 'parameter'...)}.
Usage example of U method:
U('User/add') // Generate the add operation address of the User module
Can also support group calls:
U('Home/User/add') // Generate the add operation address of the User module of the Home group
Of course, you can also just write the operation name to indicate calling the current module
U('add') // Generate the add operation address of the current access module
In addition to group, module and operation names, we can also pass in some parameters:
U('Blog/read?id=1') // Generate the read operation of the Blog module and the URL address with id 1
The second parameter of the U method supports incoming parameters and supports two definition methods: array and string. If it is only a string parameter, it can be defined in the first parameter. The following methods are all equivalent. :
U('Blog/cate',array('cate_id'=>1,'status'=>1))
U('Blog/cate','cate_id=1&status=1')
U('Blog/cate?cate_id=1&status=1')
Articles you may be interested in
- String Functions in PHP Full summary
- PHP generates continuous numeric (letter) array function range() analysis, PHP lottery program function
- Summary of system constants in thinkphp’s Action controller
- In variables and The difference after adding the static keyword before the function
- PHP filter_var() function Filter function
- PHP compresses the html web page code to reduce the amount of network data transmission, clear spaces, tabs, and comment marks
- The solution to the invalid automatic verification and automatic filling of thinkphp
- A summary of the method of submitting data using curl's post and the method of getting the web page data using get in PHP

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)