Automatically load class libraries

Source: Internet
Author: User

In 3.2, there is basically no need to manually load the class library file, you can easily complete the automatic loading.

Namespaces are loaded automatically

The system can automatically navigate to class library files through the class's namespace, for example:

We have defined a class Org\Util\Auth class:

    namespace Org\util;     class Auth {    }

Save to ThinkPHP/Library/Org/Util/Auth.class.php .

Next, we can instantiate it directly.

new \org\util\auth ();

Org\Util\Authwhen instantiating a class, the system automatically loads the ThinkPHP/Library/Org/Util/Auth.class.php file.

The namespace under the framework's library directory can be automatically identified and positioned, for example:

    ├─library Framework Class Library directory    │├─think core Think Class Library package directory    │├─org ORG class Library package directory     ... More Class Library Catalogs

The subdirectory under the Library directory is a root namespace, which means that classes with Think and org as root namespaces can be loaded automatically:

    New Think\cache\driver\File();     New Org\util\auth ();     New Org\io\File();

can automatically load the corresponding class library file.

You can add any new directory under the library directory and it will automatically register as a new root namespace.

Registering a new namespace

In addition to the namespaces below the Library directory, we can also register additional root namespaces, such as:

    Array (    ' My ' = Think_path. ' My ',    ' one ' and ' = Think_path. ' One ',    )

After configuring the above AUTOLOAD_NAMESPACE , if we instantiate the following class library

    New my\net\iplocation ();     New One\util\Log();

The corresponding class library file is loaded automatically

1     Thinkphp/my/net/iplocation. class. PHP 2     thinkphp/one/util/Log. class. php

If the namespace is not under the Library directory and no corresponding parameters are defined, AUTOLOAD_NAMESPACE it is automatically loaded as the module's namespace, for example:

1     New Home\model\usermodel (); 2     New Home\event\userevent ();

Since the home directory does not exist under the Thinkphp/library directory, and the home namespace is not defined in the parameters, the AUTOLOAD_NAMESPACE home is recognized as a module namespace, so it is automatically loaded:

1     Application/home/model/usermodel. class. PHP 2     Application/home/event/userevent. class. php

Note: The case of the namespace needs to correspond to the case of the directory name, or it may fail to load automatically.

Class Library Mappings

Following our namespace definition specification above, we can basically complete the automatic loading of the class library, but if we define more namespaces, the efficiency will decrease, so we can define class library mappings for the common class libraries. Naming a class library mapping is equivalent to defining an alias for a class file that is more efficient than a namespace location, such as:

1     Think\think::addmap (' Think\log ', Think_path. think\log.php '); 2     Think\think::addmap (' Org\util\array ', Think_path. Org\util\array.php ');

You can also use the Addmap method to bulk Import class library mapping definitions, such as:

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, the more convenient way is that we can create a alias.php file under the module configuration directory to define class library mappings, which are automatically loaded and defined in the following way:

1     return Array (2     ' think\log ' = Think_path. ' Think\log.php ',3     ' org\util\array ' = ' think_path. ' Org\util\array.php '4     );

Priority for auto-loading

In the actual Application class library loading process, often involves the priority problem of automatic loading, take the Test\MyClass class as an example, the automatic loading priority order is as follows:

    1. Determine if there is a registered Test\myclass Class library mapping, if any, automatically load the class library mapping definition of the file;
    2. Determine if there is a library/test directory, and then the directory is loaded as the initial directory;
    3. Determine if there is a registered test root namespace, and the registered directory for the initial directory load;
    4. If none of the above is true, the initial directory load is done with test as the module directory;

A file that loads the corresponding path of the namespace with the initial directory obtained above;

To manually load a third-party class library

If you want to load a third-party class library, including a class library that does not conform to the naming conventions and suffixes, and a class library that does not use namespaces or namespaces and paths that are inconsistent, or if you want to load class library files manually, we can load them by manual import.

We can import any class library using the import method, with the following usage:

1     //Import ORG Class Library package library/org/util/date.class.php class library2Import ("Org.Util.Date");3     //Import the Application/home/util/userutil.class.php class library below the Home module4Import ("Home.Util.UserUtil");5     //Import the class library below the current module6Import ("@. Util.array ");7     //Import the Vendor class library package library/vendor/zend/server.class.php8Import (' Vendor.Zend.Server ');

For the import method, the location of the imported class library file is automatically recognized, and the class library package that thinkphp can automatically identify includes think, ORG, Com, Behavior, and vendor packages, and subdirectories under the Library directory. If you created a test subdirectory below the Library directory and created a UserTest.class.php class library, you can import it like this:

1     Import (' test.usertest ');

The other is that the Application class library is imported.

Note that if your class library does not use a namespace definition, you need to add the root namespace when instantiating, for example:

1     Import (' test.usertest '); 2     $test new \usertest ();

According to the rules of the system, the import method is unable to import the class library file with the dot number, because the dot will be converted directly to a slash, for example, we define a file named User.Info.class.php, using:

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

The way to load the words will be an error, causing the loaded file is not the org/user.info.class.php file, but the org/user/info.class.php file, in which case we can use:

1 import ("Org.user#info");

To import.

In most cases, the import method will automatically identify the location of the imported class library file, and if it is a special case import, you need to specify the second parameter of the import method as the starting import path. For example, to import the rbac/accessdecisionmanager.class.php file under the directory where the current file is located, you can use:

1 import ("RBAC. Accessdecisionmanager ",dirname(__file__));

If you are importing a class library file name with a suffix other than class.php instead of PHP, you can specify the suffix with the third parameter of the import method:

1 import ("RBAC. Accessdecisionmanager ",dirname(__file__),". php ");

Note: Under UNIX or Linux hosts are case-sensitive, so when using the import method, you should pay attention to the case of the directory name and the class library name, otherwise the import will fail.

If your third-party libraries are placed under the vendor directory and both have. php as the class file suffix, and you don't use namespaces, you can use the system's built-in vendor function to simplify the import. For example, we put Zend filter\dir.php under the Vendor directory, this time the Dir file path is vendor\zend\filter\dir.php, we use the Vendor method import only need to use:

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

You can import the Dir class library.

The vendor method can also support the same base path and file name suffix parameters as the import method, for example:

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

Automatically load 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.