Home  >  Article  >  PHP Framework  >  How to call a method in another class in thinkphp

How to call a method in another class in thinkphp

PHPz
PHPzOriginal
2023-04-17 09:49:111261browse

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.

  1. 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
If the current class and the imported class are not a parent-child relationship, but a cooperative relationship, we can call the class by instantiating the object of the imported class Methods. For example:

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.

Summary

The above is how to call the method of another class in ThinkPHP. Whether it is a parent-child relationship or a cooperative relationship, we can import the class to be used through the

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!

Statement:
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