thinkphp D方法 疑问

WBOY
Release: 2016-06-06 20:11:25
Original
1102 people have browsed it

我看到别人写代码的时候,直接用D("User")方法,我知道这个D方法是用来实例化自定义模型类的,但是我没有在Model目录中找到这个UserModel.class.php,请问那是怎么回事?

回复内容:

我看到别人写代码的时候,直接用D("User")方法,我知道这个D方法是用来实例化自定义模型类的,但是我没有在Model目录中找到这个UserModel.class.php,请问那是怎么回事?

没有model就会实例化 系统基础Model

<code class="php">function D($name = '', $layer = '')
{
    if (empty($name)) {
        return new Think\Model;
    }
    static $_model = array();
    $layer         = $layer ?: C('DEFAULT_M_LAYER');
    if (isset($_model[$name . $layer])) {
        return $_model[$name . $layer];
    }
    $class = parse_res_name($name, $layer);
    if (class_exists($class)) {
        $model = new $class(basename($name));
    } elseif (false === strpos($name, '/')) {
        // 自动加载公共模块下面的模型
        if (!C('APP_USE_NAMESPACE')) {
            import('Common/' . $layer . '/' . $class);
        } else {
            $class = '\\Common\\' . $layer . '\\' . $name . $layer;
        }
        $model = class_exists($class) ? new $class($name) : new Think\Model($name);
    } else {
        Think\Log::record('D方法实例化没找到模型类' . $class, Think\Log::NOTICE);
        $model = new Think\Model(basename($name));
    }
    $_model[$name . $layer] = $model;
    return $model;
}</code>
Copy after login

如果没有UserModel.class.php也是可以执行的,不过你要做关联模型就会报错找不到这个关联的模型,验证这些都是要创建UserModel.class.php来才可以进行的,如果没有这个UserModel.class.php你可以吧D当成M来使用

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!