ThinkPHP single letter function packing

Source: Internet
Author: User
ThinkPHP single letter function sorting this is an TdWeb note. with his consent, I put it up. He was originally written for the Lite version, but now these single letter functions also exist in the standard version. Therefore, these materials are also applicable in two versions of ThinkPHP. Because Tdweb is a little lazy, some code is sorted by the single-letter function of ThinkPHP.

This is an TdWeb note. I put it up with his consent. He was originally written for the Lite version, but now these single letter functions also exist in the standard version. Therefore, these materials are also applicable in two versions of ThinkPHP. Because Tdweb is a little lazy, some code is directly copied, so I did a little bit of work (mainly the page style is slightly updated, tddb is also angry when some statements are updated)
Http://bitctw.cn/hl/docs/
The original article is as follows: (I only introduce single letter functions)

All the separate functions of TP, except two buildAppDir and others used to generate the project, are in the framework directory/Common/functions. php file.

?

Some people do not like single-letter functions such as TP. In fact, this is also a feature of TP. it is very convenient to understand the functions of these functions, whether it is back or write, next we start in alphabetical order.

?

Function A (abbreviated as "Action)

Function A is used to instantiate our Action Class. for example, our program has two actions: IndexAction and TestAction. in IndexAction, there is A myHello method that can output hello world, what if I want to output the same text in TestAction? The original method first imports the file IndexAction. class. php, then new IndexAction, and finally calls the myHello method.

The code is generally:

?

PHP code
  1. ? ("@. Action. Index ");??
  2. // Import the Index. class. php file under the Action directory in the current project directory .??
  3. Class? TestAction? Extends? Action {??
  4. ???? Public? Function? Index ()??
  5. ???? {??
  6. ???????? $ Index = new? IndexAction (); // instantiate IndexAction ??
  7. ???????? Echo? $ Index-> myHello (); // call the myHello () method ??
  8. ????}??
  9. }??
  10. ?> ??

So how can we write if we use function?

?

PHP code
  1. Class? TestAction? Extends? Action {??
  2. ???? Public? Function? Index ()??
  3. ???? {??
  4. ???????? $ Index = A ("Index ");??
  5. ???????? Echo? $ Index-> myHello ();??
  6. ????}??
  7. }??
  8. ?> ??

?

Well, do not write much code. Of course, this is not the most lazy method. please refer to the R () function below to know what is lazy.

?

Function B

?

This is a new function with the emergence of behavior. it can execute an action, such as B ('app _ begin'). it is all the functions defined by this behavior before the project starts, two parameters are supported. The second parameter must accept an array, for example, B ('app _ Begin', array ("name" = & gt; "tdweb ", "time" => time.

?

C functions

Obtain the configuration value, which should be used by many users. Although easy to use, the C function is undoubtedly a very powerful function.

Get value:
Get all settings: C (); returns an array containing all settings without passing any parameters.
Obtain the specified configuration: C ('URL _ Model') to obtain the configuration information of URL_MODEL.
Obtains the specified two-dimensional array configuration: C ("array. name"), and returns the value corresponding to the key of the array as name.

Set value:
Assign a value to a two-dimensional array C ("array. name", "value"). The principle is the same as above (obtain the value of array. name), and the value behind it is the value.

?

Batch assignment :?
$ Test = array ("URL_MODEL" => 1, "THIN_MODEL" => true ");
C ($ test );
In this way, the values in the array are assigned a value.

?

Determine whether to assign values:
C ("? URL_MODEL ") add "? ", Returns true if the value has been assigned.

?

It should be noted that although the configuration value has been changed here, only the changes made on this page will not work on the next page. if you want to change it, it will be changed permanently, then, you need to use the F function to write the configuration file to config. php.

?

D function

DAO function should be the most commonly used function for writing programs. Similar to function A, if function D is not used, you need to import the Model, and then new Model. The rest are the same.

?

However, the D function has two features. One is that if this Model has been instantiated before, it will not be instantiated and there will be resources left. The other is convenient debugging. if this Model does not exist, will throw TP exception, very user-friendly.

If you access the Model of this project, you can directly access D ("Model name"). If you want to access the Model across projects, you can use D ("Model name", "project name "); nothing else.

?

F function

Fast reading and saving of file data

?

Save Data quickly: F ("mydata", "Here is the Data to be saved"). In this way, the project Data directory stores a file named mydata. the content in the php file is the second parameter of the function.

?

Specify the storage time: F ("mydata", "Here is the data to be saved", "60"). In this way, if you access, if the interval is greater than 60 seconds, the cached files are deleted.

?

Specify the storage directory: F ("mydata", "Here is the data to be saved", "60", DATA_PATH ).

Immediately delete the cache: F ("mydata", null), the second parameter passes a null value, so that the cache mydate is deleted.

?

Read the cached data: F ("mydata ").

?

L function

?

Language-defined functions. L ("intro") gets the language defined as intro. l ("intro", "Introduction") assigns values to intro, the same principle is true for C functions.

?

R functions

?

Remember our A function. if you only want to execute A method, it is more convenient to use the R function. you can replace it

PHP code
  1. Class? TestAction? Extends? Action {??
  2. ???? Public? Function? Index ()??
  3. ???? {??
  4. ???????? $ Index = R ("Index", "myHello ");??
  5. ???????? Echo? $ Index ;??
  6. ????}??
  7. }??
  8. ?> ??

? Is it simpler?

?

S function

The global cache read/write function is similar to C, but is directly written as a file. it is written in the Temp directory. However, note that if the cache name is aaa, the cached file name is the md5 ("aaa") value, which is worth noting.

U function

?

The U function is a very powerful function. it is mainly used for URL assembly and supports different modes and routes.

?

For example:
Obtain the Action address of the current module: U ("/nowMethod ");
Obtain the Action address of the current module and pass the parameter U ("/nowMethod? Params = test ");

?

(If you are not used to the above method, you can use U ("/nowMethod", array ("params" => "test"); to pass parameters in an array, the results are the same)

Method for accessing Other modules: U ("Other/otherMethod ").
Cross-Project Access: U ("appname: // Other/otherMethod ");
Use Route Access: U ("appName: // routeName @ moduleName/actionName? Params ");

?

In addition, if you want to directly jump, write 1 in the second parameter, for example, U ("/nowMethod", 1); then directly transfer to the specified URL.

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.