thinkphp automatically load

In 3.2, basically having to manually load the library file, you can easily complete the automatic loading.

Namespace automatically load

The system can automatically locate a namespace library class files, for example:

We define a class  Org\Util\Auth category:

  1. namespace Org\Util;
  2. class Auth {
  3. }

To save  ThinkPHP/Library/Org/Util/Auth.class.php.

Next, we can directly instantiated.

  1. new \Org\Util\Auth();

In the instance of Org\Util\Auththe class, the system will automatically load the  ThinkPHP/Library/Org/Util/Auth.class.php file.

Library directory name space frame can automatically identify and locate, for example:

  1. ├─Library 框架类库目录
  2. ├─Think 核心Think类库包目录
  3. ├─Org Org类库包目录
  4. ├─ ... 更多类库目录

Library directory is a subdirectory of the root namespace, that is to say Think, Org root namespace class can be loaded automatically:

  1. new Think\Cache\Driver\File();
  2. new Org\Util\Auth();
  3. new Org\Io\File();

You can automatically load the corresponding library file.

You can arbitrarily add a new directory under the Library directory, it will automatically register as a new root namespace.

Register a new namespace

In addition to the Library directory name space outside, we can also register other root namespace, for example:

  1. 'AUTOLOAD_NAMESPACE' => array(
  2. 'My' => THINK_PATH.'My',
  3. 'One' => THINK_PATH.'One',
  4. )

The above configuration AUTOLOAD_NAMESPACEafter, if we instantiate the following libraries

  1. new My\Net\IpLocation();
  2. new One\Util\Log();

It will automatically load the corresponding library file

  1. ThinkPHP/My/Net/IpLocation.class.php
  2. ThinkPHP/One/Util/Log.class.php

If the namespace is not in the list below Library, corresponding to the definition and no AUTOLOAD_NAMESPACEargument, as namespace will be automatically loaded module, for example:

  1. new Home\Model\UserModel();
  2. new Home\Event\UserEvent();

Home directory does not exist since ThinkPHP / Library directory, nor in AUTOLOAD_NAMESPACEthe definition of Home namespace parameters, so as to put Home module namespace identified, it will be automatically loaded:

  1. Application/Home/Model/UserModel.class.php
  2. Application/Home/Event/UserEvent.class.php

Note: namespace capitalization needs and capitalization of a directory name corresponds to, or it may fail to load automatically.

Library map

We follow the namespace definition above specification, it can basically complete class library automatically load, but if the definition of more namespaces, then efficiency will decline, so we can give the definition of common library library mapping. Corresponds to the class name mapping library file defines an alias name space efficiency than a more efficient targeting, for example:

  1. Think\Think::addMap('Think\Log',THINK_PATH.'Think\Log.php');
  2. Think\Think::addMap('Org\Util\Array',THINK_PATH.'Org\Util\Array.php');

The method may also be used addMap bulk import library mapping definition, for example:

  1. $map = array('Think\Log'=>THINK_PATH.'Think\Log.php','Org\Util\Array'=>THINK_PATH.'Org\Util\Array.php');
  2. Think\Think::addMap($map);

Of course, more convenient way is that we can configure the modules directory is created alias.php library file is used to define the mapping, the file will automatically load, defined as follows:

  1. return array(
  2. 'Think\Log' => THINK_PATH.'Think\Log.php',
  3. 'Org\Util\Array' => THINK_PATH.'Org\Util\Array.php'
  4. );

Automatic loading priority

In practical applications, the process of loading the library, often related to the priority of the automatic loading, in Test\MyClassthe Example, priority automatically loaded as follows:

  1. Determine whether there is registered Test \ MyClass mapping library, if the library is automatically loaded mapping definition file;
  2. Determines whether there Library / Test directory, there are places in the directory is loaded initial directory;
  3. Determining whether a root namespace Register Test, have registered places directory is loaded initial directory;
  4. If the above does not hold, places Test initial directory is loaded as modules directory;

In the above acquired initial loading directory file path corresponding to the namespace;

Manual loading third-party libraries

If you want to load third-party libraries, including inconsistent with naming and library suffix, and does not use a namespace name or path library and space inconsistent , or if you just want to manually load the library file, we can manually import It loaded fashion.

We can use the import method to import any class libraries, used as follows:

  1. // 导入Org类库包 Library/Org/Util/Date.class.php类库
  2. import("Org.Util.Date");
  3. // 导入Home模块下面的 Application/Home/Util/UserUtil.class.php类库
  4. import("Home.Util.UserUtil");
  5. // 导入当前模块下面的类库
  6. import("@.Util.Array");
  7. // 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
  8. import('Vendor.Zend.Server');

For the import method, the system will automatically identify the location of the import library file, ThinkPHP can automatically identify the library package includes Think, Org, Com, Behavior and Vendor packages, and Library directory subdirectories, if you create a directory under Library a Test subdirectory, and create a UserTest.class.php library, so you can import:

  1. import('Test.UserTest');

Others that it is the application of the import library.

Note, if your library does not use a namespace definition, instantiation need to add root namespace, for example:

  1. import('Test.UserTest');
  2. $test = new \UserTest();

In accordance with the rules of the system, import method is unable to import library file with the number of points, because the number of points will be directly converted into a slash, for example, we define a name for the file User.Info.class.php words, the use of:

  1. import("Org.User.Info");

Way to load, then there will be mistakes, resulting file is not loaded Org / User.Info.class.php file, but Org / User / Info.class.php file, in which case, we can use:

  1. import("Org.User#Info");

To import.

In most cases, import method can automatically identify the location of the file to import library, if it is introduced into the special circumstances, you need to specify the method of the second import parameter as starting introduction path. For example, the current directory where the file you want to import the following RBAC / AccessDecisionManager.class.php file, you can use:

  1. import("RBAC.AccessDecisionManager",dirname(__FILE__));

If the suffix you want to import library file is not class.php but php, you can use the import method in the third parameter specifies the suffix:

  1. import("RBAC.AccessDecisionManager",dirname(__FILE__),".php");

Note: In the following Unix or Linux host is case-sensitive, so when using the import method to pay attention to capitalization of a directory name and the name of the library, otherwise the import fails.

If your third-party libraries are placed in the Vendor list below, and all end with the .php file extension type, the use of namespaces is useless, then you can use the built-in system functions Vendor simplify import. For example, we Zend's Filter \ Dir.php put Vendor directory, this time path Dir file is Vendor \ Zend \ Filter \ Dir.php, we use the vendor only need to use the import method:

  1. Vendor('Zend.Filter.Dir');

You can import the library Dir.

Vendor support and method can also import the same way as the base path and file name suffix parameters, such as:

  1. Vendor('Zend.Filter.Dir',dirname(__FILE__),'.class.php');

Guess you like

Origin www.cnblogs.com/furuihua/p/11759779.html