Thinkphp internal function ADSLCFUI shortcut method full parsing _ PHP Tutorial

Source: Internet
Author: User
The internal function ADSLCFUI of Thinkphp is completely parsed. ThinkPHP defines shortcuts for some common operations. these methods have single-letter method names and are easy to remember. The word ThinkPHP of these shortcuts defines shortcuts for some common operations. these methods have single-letter method names and are easy to remember. It is very interesting that the letters of these shortcuts contain ADSL letters, so we call them the ADSL method. Shortcut methods A, D, S, L, C, F, U, and I are all in/THINKPHP/Common/functions. in the php file, I will explain their respective functions and usage respectively.

A () load Action Class
D () load Model class
S () global cache configuration
L () obtain language definitions
C () get the configuration value
F () fast file data reading and saving for simple types of data strings, arrays
U () is used to assemble URL addresses.
I () quickly create an object instance

1. A. quickly create an Action object

$ Action = new UserAction (); // equivalent to the following statement: $ action = A ("User"); and if the current UserAction class has not been introduced, method A is automatically introduced. It also supports the Singleton mode and does not create the same Action object 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
 
Define the model class, such as UserModel, and then use the D () function to operate the data. For example:
 
First, create a php script named UserModel. class. PHP under "your project"/Lib/Model. the content is as follows:
 
Class UserModel extends Model {}

Then, you can perform the following operations without adding any attributes and methods:
 
$ User = D ("User"); // instantiate the User object. User is a data table created in the database named "prefix _ user, you can also use $ User = new UserModel () instead to instantiate an object. After instantiation, you can add, delete, query, modify, and perform a series of operations on the data, such:
 
$ User-> find (1); // query records whose primary key is 1

We need to add 1 to the specified field when making user gold coins, points, or votes. at this time, we can write

$ User-> score = '(score + 1)'; $ s-> save (); this saves a lot of steps.

If you want to modify a specified field, it can be abbreviated as follows:

D ('user')-> setField ('name', 'he', 'id = 2 ');

The main differences between the D and M methods are:

The M method does not need to create a model file, and the M method does not read the model class. by default, automatic verification is invalid, but it can be implemented through dynamic assignment; the D method must have a model class. we can use the following two methods to create a ing object for a data table.

First: $ Test = D ('test ');

Type 2: $ Test = new Model ('test ');

Although the two methods can perform select, insert, delete, and udpate operations on data, there is a big difference in data verification. in the first way, the data check function is provided when a model is instance, for example, you can define that if the title is not entered, the system will prompt "enter the title" (this is an automatic verification function provided by tp, of course, you also need to define the verification conditions in the corresponding model );

The D method can automatically detect the model class. if it does not exist, an exception is thrown. At the same time, the instantiated model will not be repeatedly instantiated (single instance ). The default D method can only call 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 ();

It is equivalent:

$ Demo = M ();
 
3. S quick cache operation method
 
ThinkPHP abstracts various cache methods into uniform cache classes for calling, and ThinkPHP unifies all cache mechanisms into one S method for operations, therefore, when using different caching methods, you do not need to pay attention to the specific cache details. For example:

S ('data', $ data); // use the Data identifier to cache $ data Data
S ('data', $ data, 3600); // cache $ Data for 3600 seconds
$ Data = S ('data'); // Obtain cached data
S ('name', null); // deletes the cache ID name
 
4. L quick operation of language variables

L The method supports multiple languages and allows you to quickly set and obtain language definitions.

L ('User _ info', 'user information'); // set the language variable named USER_INFO
L ('User _ info'); // gets the language variable value of USER_INFO
// Assign values in batches
$ Array ['User _ info'] = 'User information'; $ array ['error _ info'] = 'Error information ';
L ($ array );

5. C: quick operation of configuration variables. the usage is C ("fill in the subscript of the array in the configuration file ")

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

Same as L, C also supports batch assignment.
 
Note: configuration parameters are case insensitive.

In addition, from version 1.5, the C method also supports two-dimensional array operations, for example:
 
C ('user. USER_TYPE ', 1 );
C ('user. USER_AUTH_ON ');

6. F file data storage method

The F method is mainly used to write, change, and delete file data of a project. its working mechanism and S method are similar. The difference lies in the different purposes and different data storage directories, you cannot specify the cache mode because data is stored as files by default. The F method uses the var_export method, so it only supports simple data types and does not support object caching.

7. U is used to assemble URL addresses. It is characterized by the ability to automatically generate the corresponding URL address based on the current URL mode and settings.

The function format is U ('address', 'parameters', 'pseudo-static ', 'redirect?', 'display domain name '); the advantage of using the U method in the template instead of writing the dead URL address is that once your environment changes or the parameter settings change, you do not need to change any code in the template. The call format in the template must use {: U ('address', 'parameters '...)} .

U method Usage example:

U ('user/add') // Generate the add operation address of the User module

You can also support group calls:

U ('Home/User/add') // Generate the add operation address for the User module of the Home group

Of course, you can only write the operation name to call the current module

U ('ADD') // Generate the add operation address of the current access module

In addition to group, module, and operation name, we can also input some parameters:

U ('blog/read? Id = 1') // The URL of the read operation that generates the Blog module and whose id is 1

The second parameter of the U method supports the input parameter and the array and string defining methods. if the parameter is just a string, it can be defined in the first parameter. the following methods are 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
  • Summary of String Functions in PHP
  • PHP generates continuous numbers (letters) array function range () analysis, PHP lottery program function
  • Summary of system constants in the Action Controller of thinkphp
  • Differences between variables and functions in php after the static keyword is added
  • PHP filter_var () function Filter function
  • PHP compresses html webpage code to reduce the amount of network data transmitted, clear spaces, tabs, and comment mark
  • Thinkphp automatic verification and automatic filling are invalid solutions
  • In php, we use the curl post method to submit data and the get method to obtain webpage data.

Bytes. It is very interesting that the words of these shortcuts...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.