search
HomePHP FrameworkThinkPHPAnalysis of library extension operations such as Thinkphp framework extension

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 {
}

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

$image = new \Org\Util\Image;

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 {
}

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

namespace Com\Sina;
class Rank {
}

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',
)

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 {
}

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

namespace Lib\Sina;
class Rank {
}

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 {
}

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

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

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 {
}

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

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

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
This article is reproduced at:jb51. If there is any infringement, please contact admin@php.cn delete

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.