Thinkphp Learning note 9-automatic loading

Source: Internet
Author: User
Tags php class zend

Original: thinkphp Learning note 9-automatic loading

1. Automatic namespace loading

In version 3.2, you do not need to manually load the class library files, it is convenient to complete the automatic loading.

The system can automatically navigate to a class library file based on the namespace of the class, such as defining a class Org\util\auth class:

namespace Org\util; class Auth {}

Save to Thinkphp/library/org/util/auth.class.php

So that we can instantiate it directly,

new \org\util\auth ();

The system will automatically load after instantiation

thinkphp/library/org/util/auth.class.php

The namespaces in the framework liberary directory can be automatically identified and positioned as follows

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

Subdirectories under the Library directory are all a root namespace, meaning that classes under think,org 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.

We can also add any new directory under the Liberary directory, and it will be automatically registered as a new root namespace, as follows:

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

After configuring the above Autoload_namespace, you can instantiate the following class library

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

Automatically load the following class library file

thinkphp/my/net/iplocation.class.php
thinkphp/one/util/log.class.php

If the namespace is not under the Library directory and the corresponding auto_loadnamespace parameter is not defined, it is automatically loaded as the module's namespace, for example

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

This jump is a bit big, just said or liberary under the name space, and now pull to application under.

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

Application/home/model/usermodel. class. phpapplication/home/event/userevent. class. php

These namespaces seem to be in the Thinkphp\liberary directory, the contents of this directory is generally not modified Ah, do not understand why not say application directory namespace.

2. Class Library Mapping

Defining a greater number of namespace efficiencies can have an effect on defining class library mappings for commonly used class libraries, which are equivalent to defining an alias for a class file, such as:

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

Where should this paragraph be written, the documentation is not clear. You can also bulk import a class library mapping definition using the Addmap method, as follows:

$map Array (' Think\log ' =>think_path. ' Think\log.php ', ' Org\util\array ' =>think_path. ' Org\util\array.php '); Think\think::addmap ($map);

The document still does not indicate where this paragraph should be written, whether it is config.php or the entrance file, catch nasty ah.

3. Priority of Class library loading

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

1. Determine if the Test\myclass Class library mapping is registered, and if so, automatically load the file for the class library mapping definition.

2. Determine if there is a liberary\test directory, and then load the directory as the initial directory

3. Determine if the test root namespace is registered, and the original directory is loaded with the registered directory

4. If none of the above is true, the test module module is loaded by the initial directory.

4. Manually load a third-party class library

When using a third-party class library, the following situations may occur, do not conform to the thinkphp namespace and suffix, do not use namespaces, or the namespaces and paths are inconsistent, and we can load them using a manual import.

We import the class libraries using the import method, as follows:

// Import the Org Class Library package library/org/util/date.class.php class Library import ("Org.Util.Date"); // Import the Application/home/util/userutil.class.php class library ("Home.Util.UserUtil") below the Home module ; //  Import ("@. Util.array "); // Import the Vendor class Library package library/vendor/zend/server.class.phpimport (' Vendor.Zend.Server ');

For the import method, the system will automatically identify the location of the imported class library files, thinkphp can automatically identify the class library including Think,org,com,behavior,vendor, as well as subdirectories under the Liberary directory, this does not tear Duzi Ah, Think, Org,com,behavior,vendor just under Thinkphp\liberary, what does the Liberary directory refer to in the document? Well, it's possible that the documentation says it's a new directory of your own in the Liberary directory.

If a new test directory is created under the Liberary directory and you create a UserTest.class.php file, you can import it like this:

Import (' test.usertest ');

Note If you do not use namespace to define namespaces, you need to use the root namespace when instantiating, as follows:

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

According to the system rules, 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, if we define a User.Info.class.php file, import ("Org.User.Info"); The way to load will be an error, causing us to load not the org/user.info.class.php file, but the org/user/info.class.php file, in which case we use the import ("Org.user#info"); mode, which means that the number of points in the file name is replaced by #.

In most cases, import can automatically identify the location of the imported class library file, and in special cases a second parameter is required to specify the path of the starting import, for example: To import the rbac/accessdecisionmanager.class.php file in the directory where the current file is located, You can use import ("RBAC. Accessdecisionmanager ", DirName (__file__));. If the file suffix you want to import is not class.php but. PHP, you can use the third parameter import ("RBAC. Accessdecisionmanager ", DirName (__file__),". php ");

If a third-party class library is placed in the vendor directory, with. php as the file suffix, and no namespace, the vendor function inside the system can be used to simplify the import, for example, we want to put Zend's filter\ Dir.php placed under the vendor directory, this time the Dir file path is vendor\zend\filter\dir.php, using the vendor method to import the following:

Vendor (' Zend.Filter.Dir ');

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

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

The feeling is high, these seem to be to expand the system when used more.

Thinkphp Learning note 9-automatic loading

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.