Home > Backend Development > PHP Tutorial > Thinkphp internal function ADSLCFUI shortcut method full analysis_PHP tutorial

Thinkphp internal function ADSLCFUI shortcut method full analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:31:00
Original
991 people have browsed it

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764160.htmlTechArticleThinkPHP defines shortcut methods for some commonly used operations. These methods have single-letter method names, which are easier to remember. characteristics. What’s very interesting is that the words for these shortcuts...
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