D 方法用於實例化自定義模型類,封裝 Model 實例化,實現單例模式,支援跨項目/分組調用。
D('[項目://][分組/]模型', '模型層名稱')
Model 基類;// 當前項目 User 模型 (Lib/Model/UserModel.class.php)
$User = D('User');
// 等效於
import('@.Model.UserModel');
$User = new UserModel();
// 若 UserModel 不存在,自動退回 new Model('User')
// 跨項目/分組
D('Admin://User'); // Admin 項目 User 模型
D('Admin/User'); // Admin 分組 User 模型
// 分層模型 (ThinkPHP 3.1+)
$User = D('User', 'Service'); // Lib/Service/UserService.class.php
$User = D('User', 'Logic'); // Lib/Logic/UserLogic.class.php
// 等效於
import('@.Service.UserService');
$User = new UserService();
優勢: D 方法結合「自動檢測回退 + 單例 + 分層支援」,是 ThinkPHP 業務邏輯封裝的核心入口,推薦作為模型層首選調用方式。