Home >PHP Framework >ThinkPHP >How to call a method in another class in thinkphp
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.
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().
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!