Home > PHP Framework > ThinkPHP > Analysis of library extension operations such as Thinkphp framework extension

Analysis of library extension operations such as Thinkphp framework extension

coldplay.xixi
Release: 2020-06-09 12:02:17
forward
2907 people have browsed it

Analysis of library extension operations such as Thinkphp framework extension

Detailed explanation of library extension operations such as Thinkphp framework extension

This article mainly introduces It understands the Thinkphp framework extension and other class library extension operations, and analyzes the relevant principles, implementation methods and operating precautions of Thinkphp class library extension in the form of examples. Friends in need can refer to the examples in this article

Describes the Thinkphp framework extension and other library extension operations. Share it with everyone for your reference, the details are as follows:

Library class extension

ThinkPHP’s class library mainly includes public class libraries and application class libraries, all based on namespaces defined and expanded. As long as it is defined according to the specification, automatic loading can be achieved.

Public library classes

Public class libraries usually refer to the class libraries under the ThinkPHP/Library directory, for example:

Think directory: System core class library
Org directory: Third-party public class library

The class libraries under these directories can be loaded automatically. You only need to put the corresponding class library into the directory and then add or Modify the namespace definition. You can add an Image.class.php file under the Org/Util/ directory, and then add the namespace as follows:

namespace Org\Util;
class Image {
}
Copy after login

In this way, you can directly instantiate the Image class in the following way:

$image = new \Org\Util\Image;
Copy after login

In addition to these directories, you can add your own class library directory under the ThinkPHP/Library directory. For example, we add a Com directory for enterprise class library expansion:

Com\Sina\App Class (located in Com/Sina/App.class.php)

namespace Com\Sina;
class App {
}
Copy after login

Com\Sina\Rank class (located in Com/Sina/Rank.class.php)

namespace Com\Sina;
class Rank {
}
Copy after login

Public class library except in In addition to the system's Library directory, you can also customize other namespaces. We only need to register a new namespace and add the following setting parameters in the application or module configuration file:

'AUTOLOAD_NAMESPACE' => array(
  'Lib'   => APP_PATH.'Lib',
)
Copy after login

We are in the application directory A Lib directory is created below to place public Lib extensions. If we want to put the above two class libraries under the Lib\Sina directory, we only need to adjust it to:

Lib\Sina\App class (located Lib/Sina/App.class.php)

namespace Lib\Sina;
class App {
}
Copy after login

Lib\Sina\Rank class (located in Lib/Sina/Rank.class.php)

namespace Lib\Sina;
class Rank {
}
Copy after login

If your class library does not use naming If there is space, you need to use the import method to load the class library file first, and then instantiate it. For example: We define a Counter class (located in Com/Sina/Util/Counter.class.php):

class Counter {
}
Copy after login

In When using it, you need to call it in the following way:

import('Com.Sina.Util.Couter');
$object = new \Counter();
Copy after login

Application class library

The application class library is usually a class library under the application or module directory. The namespace of the application class library is generally the module. The name is the root namespace, for example: Home\Model\UserModel class (located in Application\Home\Model)

namespace Home\Model;

use Think\Model;

class UserModel extends Model{

}

Common\Util\Pay class (located in Application\Common\Util)

namespace Common\Util;
class Pay {
}
Copy after login

Admin\Api\UserApi class (located in Application\ Admin\Api)

namespace Admin\Api;
use Think\Model;
class UserApi extends Model{
}
Copy after login

Remember a principle. If the namespace path corresponds to the actual file path, automatic loading can be achieved during direct instantiation.

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of Analysis of library extension operations such as Thinkphp framework extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template