-2

快速提问。我有 mvc(我的迷你框架;))用于教育。

https://github.com/aras123/MiniFramework

如果我在根目录中创建文件 test.php(例如)。

测试.php

<?php 
class Example {
    public function __construct() {
    echo 'This is example!';
    }
}

在我的框架中为 IndexController 创建操作并希望包含和运行类

应用程序/控制器/IndexController.php

<?php
namespace Application\Controller;
use Framework\Controller;

class IndexController extends Controller {
    public function _init() {
    }

    public function IndexAction() {
        require 'test.php'; //path is ok
                $aaa = new Example(); //is error 
    }

错误信息:

Fatal error: Class 'Application\Controller\Example' not found in
/Application/Controller/IndexController.php on line ...
4

2 回答 2

1

They way your autoloader works, its not picking up the file you created. Theres two solutions here, either you have an include at the top of your controller

<?php
namespace Application\Controller;
require __DIR__ . '/../../test.php';
// ...

Or, you move test.php to Application\Controller and rename it to Example.php

I would suggest the latter

于 2013-09-05T18:29:55.903 回答
0

是的!这是很好的代码:

<?php
namespace Application\Controller;
use Framework\Controller;

class IndexController extends Controller {
    public function _init() {
    }

    public function IndexAction() {
        require 'test.php'; //path is ok
                $aaa = new \Example(); //now is ok!
    }
于 2013-09-05T21:53:02.827 回答