search
HomePHP FrameworkThinkPHPHow to pass parameters in thinkphp D function

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:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Safe Exam Browser

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools