Home  >  Article  >  PHP Framework  >  How to pass parameters in thinkphp D function

How to pass parameters in thinkphp D function

王林
王林forward
2023-06-02 19:32:09967browse

1. Introduction to D function

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

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

Among them, the model name is a 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 order to better handle the business logic, in During development, we may need to pass parameters to specific methods of the model. The following code can be used to query the list of users older than 20 years old

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

getUsersByAge in the above code is a custom User model method used to query user information by 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. To instantiate a model object in another application, simply specify the name of the application. For example, to create an instance of the User model object in the Admin application, you can use the following code:

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

In the above code, 'Admin' represents the application name. In this case, the Admin application searches the User model to instantiate the object, allowing the D function to function properly. However, we still need to find a way to pass parameters to the getUsersByAge method in the User model.

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. The following code can be used to query the list of users over 20 years old

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

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

3. Example

We try to create a module named Blog in the ThinkPHP5.0 project for actual operation. 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 used 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. By setting the query conditions using the where method, we finally return the query results through the select method.

The above is the detailed content of How to pass parameters in thinkphp D function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete