php文件无法找到接收器'类

时间:2017-06-17 23:43:45

标签: php

我有两个php文件,一个视图(内容)和一个Object(Receiver)。

content.php

<?php

include '../model/Receiver.php';

$receiver = new Receiver;

Receiver.php

<?php

    namespace app\model;

    use app\model\Invoice;

    class Receiver{

        public function __construct(){
            $Invoice = new Invoice;
        }

    }

项目结构

.
├── app

│   ├── model
         └── Receiver.php
│   └── view
         └── content.php

运行content.php后,我收到此错误。

  

PHP致命错误:未捕获错误:类&#39;接收器&#39;找不到   C:\ MAMP \ htdocs中\ invoiceexpress \应用\视图\ content.php:5

我无法找出原因。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

因为您通过Receiver将命名空间添加到namespace app\model;类。使用new或使用use语句时可以使用添加命名空间:

<?php

include '../model/Receiver.php';

use \app\model\Receiver;

$receiver = new Receiver;

<?php

include '../model/Receiver.php';

$receiver = new \app\model\Receiver;