ThinkPHP function description: import method

Source: Internet
Author: User
Tags oauth
The second and third parameters of the import method are not allowed in the alias import method. the alias import method is more efficient than the namespace import method, the disadvantage is that related aliases need to be pre-defined. The import method is the encapsulation implementation of the ThinkPHP framework for class library import, especially for project class libraries, extended class libraries, and third-party class libraries, earlier versions of the import method can import directories and wildcards like the import method of java. later, considering performance issues, the import method is constantly improved and simplified in subsequent version updates, therefore, the current usage is simple and clear. Call format:
Import ('class Library name', 'Start path ', 'class Library suffixes ')
The imprt method has an alias for the vendor method, which is used to import third-party class libraries. The difference is that the default value of the starting path and class library suffix is different.

Let's analyze the specific usage: importing the system Base Class Library actually refers to the Think class library package, and the directory refers to the core Lib Directory of the framework, the import method can be used to import the system Base Class Library, for example:
  1. Import ('Think. Util. array ');
Copy the code to import the Lib/Util/Array. class. php class library file under the system directory.
  1. Require THINK_PATH. 'Lib/Util/Array. class. php ';
Code replication supports multi-level directories, for example:
  1. Import ('Think. Util. U1.ClassA ');
Copy code
  1. Import ('Think. Util. U1.A2. ClassB ');
Copy the code to import the class library through the import method, and then you can instantiate the class library. The import extension class Library is located under the Extend/Library Directory, which is the system's public Extension class Library directory. Currently, only the ORG and Com packages are supported.
  1. Import ('org. Util. image ');
  2. Import ('com. Sina. OAuth ');
Copy the code to import the third-party class libraries (Extend/Library/ORG/Util/Image) under the extension directory. class. php and Extend/Library/Com/Sina/OAuth. class. php class library files), third-party class library packages can only support ORG and Com, the following sub-directories can be added at will. If the start import path is not specified for the imported project application class library, all the class library packages other than Think, ORG, and Com will be considered as the imported project application class library. for example:
  1. Import ("MyApp. Action. UserAction ");
  2. Import ("MyApp. Model. InfoModel ");
Copy the code to import the UserAction and InfoModel class library files of the MyApp project. since we usually import the class libraries under the current project, the code can be abbreviated:
  1. Import ("@. Action. UserAction ");
  2. Import ("@. Model. InfoModel ");
Copy the code @ symbol to import the class libraries under the current project. this method also facilitates code migration of the project class library to a certain extent. if the project name is changed or moved to another project, the writing method does not need to be changed. Import a non-standard class library file the non-standard class library file mentioned here mainly refers to the class library file located in a special location or not. class. php suffix. For example, the imported base class libraries, extended class libraries, and project class libraries are all in the directory based on the framework specification. if you need to import the MyClass. php file under the Common Directory of the project, you can use:
  1. Import ('Common. myclass', APP_PATH, '. php ');
Copy the code or
  1. Import ('myclass', APP_PATH. 'common', '. php ');
Copy the code or import the RBAC class library under the current directory
  1. Import ("RBAC. AccessDecisionManager", dirname (_ FILE _), ". php ");
There is also a special case for copying code, which is the particularity of class library naming. According to the system rules, the import method cannot import a class library file with a dot number, because the DOT number is directly converted into a diagonal line. for example, we have defined a name as User. info. class. php files:
  1. Import ("ORG. User. Info ");
If you copy the code to load the file, an error occurs, causing the file to be loaded not ORG/User. info. class. php file, but ORG/User/Info. class. php file. in this case, we can use:
  1. Import ("ORG. User # Info ");
Copy the code to import it. The base class libraries imported to ThinkPHP by third-party class libraries are. class. php is a suffix, which is a built-in convention of the system. of course, it can also be controlled by the import parameter. to facilitate the introduction of other frameworks and system class libraries, the system also provides an alias for the import method, which is used to import third-party class libraries. the default start directory differs from the class file suffix. The third-party class library is located in the Vendor directory under the system extension directory. for example, we use the Zend Filter \ Dir. put php under the Vendor Directory. at this time, the path of the Dir file is Vendor \ Zend \ Filter \ Dir. php. we only need to use the following to import data using the vendor method:
  1. Vendor ('zend. Filter. dir ');
Copy the code to import the Dir class library.
The Vendor method also supports the same basic path and filename suffix parameters as the import method, for example:
  1. Vendor ('zend. Filter. dir', dirname (_ FILE _), '. class. php ');
In addition to the namespace import method, the import method also supports alias import. to use alias import, you must first define the alias. you can add alias under the project configuration directory. php defines the class library alias to be used in the project, for example:
  1. Return array (
  2. 'Rbac '=> LIB_PATH. 'common/rbac. class. php ',
  3. 'Page' => LIB_PATH. 'common/page. class. php ',
  4. );
Copy the code so you can use it directly now:
  1. Import ("rbac ");
  2. Import ("page ");
Copy the code to import the Rbac and Page types. the alias import method prohibits the use of the second and third parameters of the import method. the alias import method is more efficient than the namespace import method, the disadvantage is that related aliases need to be pre-defined.
You can define aliases for some required class libraries, so you can quickly automatically load without defining an automatic loading path.

Generally, the framework uses the automatic loading method. In most cases, you do not need to manually import the class library file. Generally, this method is used to import extension class libraries and third-party class libraries. In addition, the alias definition and the automatic loading path definition can also reduce the manual import of class libraries.

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.