Home >Backend Development >PHP Tutorial >mvc - When learning thinkPHP and using the D method, why is Common/Model called instead of Home/Model? Where is it set?

mvc - When learning thinkPHP and using the D method, why is Common/Model called instead of Home/Model? Where is it set?

WBOY
WBOYOriginal
2016-07-06 13:54:091049browse

Learn thinkPHP and use D method, why call Common/Model instead of Home/Model?

Externally called function:

<code>namespace Home\Controller;
use Think\Controller;

public function model_D()
{
    $stmt=D('shop');
    $stmt->say();
}</code>

Two types of files:

<code>被调用的是这个:
C:\AppServ\www\yb1\yangbins\Common\Model\shopModel.class.php
<?php
namespace Common\Model;
use Think\Model;

class shopModel extends Model
{
    function say()
    {
        echo 'i am in namespace Common\Model <br>()';
    }
}
</code>
<code>为什么不是这个?
C:\AppServ\www\yb1\yangbins\Home\Model\shopModel.class.php
<?php
namespace Home\Model;
use Think\Model;

class shopModel extends Model
{
    function say()
    {
        echo 'i am in namespace Home\Model <br>';
    }
}</code>

Reply content:

Learn thinkPHP and use D method, why call Common/Model instead of Home/Model?

Externally called function:

<code>namespace Home\Controller;
use Think\Controller;

public function model_D()
{
    $stmt=D('shop');
    $stmt->say();
}</code>

Two types of files:

<code>被调用的是这个:
C:\AppServ\www\yb1\yangbins\Common\Model\shopModel.class.php
<?php
namespace Common\Model;
use Think\Model;

class shopModel extends Model
{
    function say()
    {
        echo 'i am in namespace Common\Model <br>()';
    }
}
</code>
<code>为什么不是这个?
C:\AppServ\www\yb1\yangbins\Home\Model\shopModel.class.php
<?php
namespace Home\Model;
use Think\Model;

class shopModel extends Model
{
    function say()
    {
        echo 'i am in namespace Home\Model <br>';
    }
}</code>

Using the D method, the class will not be found according to the path specified by the namespace.
D method is to load the first found Class according to priority, and the order starts from comon.

There are two ways to solve your problem.

<code>$model = D("Home/Shop");</code>

Use command space

<code>use Home\Model\shopModel;
$model = new shopModel();</code>

Shouldn’t external calls specify use HomeModelshop?

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