Home  >  Article  >  PHP Framework  >  Let’s talk about how to pass parameters in thinkphp D function

Let’s talk about how to pass parameters in thinkphp D function

PHPz
PHPzOriginal
2023-04-07 09:01:17650browse

In ThinkPHP, the D function is a convenient method that can help developers quickly instantiate a model object. In many cases, we need to pass parameters to a method of the model in order to better handle the business logic. This article will introduce how to pass parameters in D functions.

1. Introduction to D function

D function is a commonly used method in ThinkPHP to instantiate model objects. Its syntax format is as follows:

D('模型名','应用名');

Among them, the model name is required parameter. The application name is an optional parameter. If not specified, it defaults to the current application. For example, if we want to instantiate the Blog model, we can use the following code:

$Blog = D('Blog');

2. How to pass parameters to the D function

In actual development, we may need to pass a method to the model Pass parameters for better handling of business logic. For example, if we need to query the list of users older than 20 years old, we can use the following code:

$User = D('User');
$list = $User->getUsersByAge(20);

In the above code, getUsersByAge is a customized method in the User model, which is used to query user information based on age. If we need to use the D function to instantiate the User model object and call the getUsersByAge method, how should we pass the parameters?

First, we need to understand the second parameter of the D function: application name. By default, the application name is the name of the current application. If we need to instantiate the model object in other applications, we can do so by specifying the application name. For example, if we want to instantiate a User model object in the Admin application, we can use the following code:

$User = D('User','Admin');

In the above code, 'Admin' represents the application name. In this way, the D function will find the User model in the Admin application and instantiate the object. However, we have not solved the problem of how to pass parameters to the User model's getUsersByAge method.

To address this problem, ThinkPHP provides a more concise way of writing. We can use the third parameter of the D function to pass parameters. For example, if we need to query the list of users older than 20 years old, we can use the following code:

$User = D('User','','');
$list = $User->getUsersByAge(20);

In the above code, the first empty string represents the model name, and the second empty string represents the application name, the third empty string indicates passing parameters. We can use the func_get_args() function in the getUsersByAge method to get the passed parameters.

3. Example

Let’s practice it in a project. In the ThinkPHP5.0 project, create a Blog module. Create an Article controller in the Blog module and add an index method to query the list of articles with an ID greater than 10 and a status of 1. The code is as follows:

// 文件地址:application\blog\controller\Article.php
namespace app\blog\controller;
use think\Controller;
class Article extends Controller
{
    public function index()
    {
        $Blog = D('Blog','Common');
        $list = $Blog->getArticlesById(10,1);
        dump($list);
    }
}

In the above code, we use the D function to instantiate a Blog model object and specify the application name 'Common'. We also called the custom method getArticlesById in the Blog model to query the list of articles that meet the conditions. The code of the getArticlesById method is as follows:

// 文件地址:application\common\model\Blog.php
namespace app\common\model;
use think\Model;
class Blog extends Model
{
    protected $table = 'blog';
    public function getArticlesById($id,$status)
    {
        $where = [
            'id' => ['gt',$id],
            'status' => $status
        ];
        return $this->where($where)->select();
    }
}

In the above code, we define a method getArticlesById that queries the list of articles with an ID greater than 10 and a status of 1. This method accepts two parameters, $id and $status. We use the where method to set the query conditions in the method, and finally return the query results through the select method.

Through the above example, we can see that the D function is very convenient to use and can help us quickly instantiate model objects and pass parameters to better handle business logic.

The above is the detailed content of Let’s talk about how to pass parameters in thinkphp D function. 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