ThinkPHP is an excellent PHP development framework and is widely loved by developers. During the development process, we often write many classes. Sometimes we need to use methods or attributes of another class in the current class, so what should we do? This article will introduce how to call a method of another class in ThinkPHP.
1. Import class
To use the method of another class, the first step is of course to introduce the class into the current class. In ThinkPHP, we can use the import
function to achieve this:
import('命名空间.类名');
Among them, namespace
and class name
are the imported classes respectively. Namespaces and class names. If the imported class is not in any namespace, just pass the class name directly to the import
function.
For example, we have a class OtherClass
, which contains a method test
. Now if we want to use this method in the current class, we can write like this:
import('app\MyClass\OtherClass'); class MyClass { public function test() { $other = new OtherClass(); $other->test(); } }
In this way, you can use the test
method in OtherClass
in MyClass
.
2. Instantiate the class
After importing the class, we also need to use the new
keyword to instantiate the class in order to use the methods and properties in the class. Normally, we instantiate the imported class in the constructor of the current class. For example:
import('app\MyClass\OtherClass'); class MyClass { private $other; public function __construct() { $this->other = new OtherClass(); } public function test() { $this->other->test(); } }
In the constructor, we instantiate OtherClass
and assign it to the private property $other
of MyClass
. Then in the test
method, we can call the test
method in the $other
object.
3. Call the method
After instantiating the imported class, we can use the methods in the class. Before calling the method, we need to understand the relationship between the current class and the imported class.
- Father-child relationship
If the current class is a subclass of the imported class, we can directly use the parent
keyword to call the method of the imported class . For example:
import('app\MyClass\OtherClass'); class MyClass extends OtherClass { public function test() { parent::test(); } }
In MyClass
, we inherit OtherClass
and override the test
method, but we also want to use ## The test
method in #OtherClass can be called using
parent::test().
- Cooperation relationship
import('app\MyClass\OtherClass'); class MyClass { private $other; public function __construct() { $this->other = new OtherClass(); } public function test() { $this->other->test(); } }In this example, there is no inheritance relationship between
MyClass and
OtherClass. We instantiate the
$other object. Call the
test method in
OtherClass.
import function, and then call its method after instantiation. For classes or methods that need to be called frequently, constants or functions can be defined in the global file of ThinkPHP to facilitate calling them anywhere in the project.
The above is the detailed content of How to call a method in another class in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
