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

Analysis of library extension operations such as Thinkphp framework extension

Jun 09, 2020 am 11:43 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
24