首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同文件中的PHP命名空间

不同文件中的PHP命名空间
EN

Stack Overflow用户
提问于 2015-05-06 04:18:41
回答 1查看 75关注 0票数 0

我正在为我的投资组合开发自己的&小型MVC框架,并为未来的开发barebone。

我遇到了一个问题,一个小时都解决不了的问题。

我正在使用composer来构建自动加载文件以及名称空间。

我有一个baseController,它现在非常简单,但我遇到的问题是,由于某些原因,我无法从我正在尝试创建的默认控制器中扩展它。

这是我的welcomeController.php文件(扩展baseController):

代码语言:javascript
复制
<?php
namespace AppWorld\Controls;
use AppWorld\FrostHeart;

class landingController extends baseController {

public function index() {

    $data['foo'] = "Foo";
    $data['bar'] = "Bar";

    $this->View->show('landingpage', $data, $showTemplate = 0);
}

}

这是我的baseController.php:

代码语言:javascript
复制
<?php
namespace AppWorld\FrostHeart;

class baseController {

    //@var::View - Instance of View object
    public $currentView;

    public function __construct() {

        //Start the session
        session_start();

        //Create new view object
        $this->currentView = new View();
    }
}

我尝试了所有的名字空间组合,因为我是如此无助,但我仍然无法解决这个问题。

welcomeController.php位于根/应用程序/控制器中,命名空间为AppWorld\Controls

baseController.php位于根/应用程序/核心名称空间中,名为AppWorld\FrostHeart。

@Edit:这就是错误:

代码语言:javascript
复制
Fatal error: Class 'AppWorld\FrostHeart\baseController' not found in D:\Software\xampp\htdocs\cms\application\controller\landingController.php on line 5

@编辑2:

composer.json

代码语言:javascript
复制
{
    "name": "simplymvc",
    "description": "Simple MVC with options to expand upon",
    "license": "MIT",
    "version": "0.0.1-dev",
    "authors": [
        {
            "name": "Maciej Olborski",
            "email": "olbi123@gmail.com",
            "homepage": "http://maciejolborski.net",
            "role": "Lead"
        }
    ],
        "require": {
            "php": ">=5.6.8"
        },
        "autoload": {
            "psr-4":{
                "AppWorld\\": "application",
                "AppWorld\\Conffy\\": "application/config",
                "AppWorld\\FrostHeart\\": "application/core",
                "AppWorld\\Controls\\": "application/controller"
            }
        }
}

@编辑3

这是Application.php:

代码语言:javascript
复制
<?php
namespace AppWorld\FrostHeart;

class Application {

    // $var::Mixed - Instance of the controller
    private $controller;
    // $var::Array - Parameters passed to the URL
    private $parameters = array();  
    // $var::String - Current controller name
    private $controller_name;
    // $var::String - Current method name
    private $method_name;

    public function __construct() {
        //Prepare URL and set URL parameters
        $this->prepareURL();
        //Check if controller and/or method are not empty
        $this->checkControllerAndMethod();

        //Check if controller file exists?
        if(file_exists(CONTROLLER_DIR . $this->controller_name . ".php")) 
        {
            //Load controller file and and create this controller
            require(CONTROLLER_DIR . $this->controller_name . ".php");

            $this->controller = new $this->controller_name();

            //Check if the method exists within this controller
            if(method_exists($this->controller, $this->method_name)) 
            {                               
                // call method and pass parameters to this method
                call_user_func_array(array($this->controller, $this->method_name), $this->parameters);
            } 
            else 
            {
                //When no parameters are given just call method without parameters
                $this->controller->{$this->method_name}();
            }
        }
    }

    private function prepareURL() {

        //GET URL and split it to URL Segments
        $params = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
        $params = trim($params, '/');
        $params = filter_var($params, FILTER_SANITIZE_URL);
        $params = explode('/', $params);

        //Set controller name to first URL Segment within GET['action']
        $this->controller_name = $params[0];
        //Set method name to first URL Segment within GET['action']
        $this->method_name = $params[1];     

        unset($params[0], $params[1]);

        //Store array of parameters(URL Segments) to parameters array
        $this->parameters = array_values($params);
    }

    /**
     * 
     * This method checks if the controller and/or method was provided in the URL 
     * If the controller and/or method was not provided then defaults are loaded from config file.
     * Also renames the Controller name so it's landingController not just landing.
     * 
     */

    private function checkControllerAndMethod() {

        //Check whether the controller_name is set if not load default controller
        if(!$this->controller_name) 
        {
            $this->controller_name = DEFAULT_CONTROLLER;
        }

        //Check whether the method_name is set if not load default method
        if(!$this->method_name || strlen($this->method_name) === 0) 
        {
            $this->method_name = DEFAULT_METHOD;
        }

        //Rename the controller_name so that it contains "Controller" word after the name
        $this->controller_name = $this->controller_name . 'Controller';

        /**
        echo 'Controller: ' . $this->controller_name;
        echo '<br />';
        echo 'Method: ' . $this->method_name;
        **/
    }
}

如果对这一行进行注释:

代码语言:javascript
复制
$this->controller = new $this->controller_name();

这一行评论如下:

代码语言:javascript
复制
$this->controller->{$this->method_name}();

然后,我可以看到所有文件被正确加载,包括基本和着陆控制器,但是如果我不对它们进行注释,错误是:

致命错误:在第27行的D:\Software\xampp\htdocs\cms\application\core\Application.php中找不到类'landingController‘

EN

回答 1

Stack Overflow用户

发布于 2015-05-06 04:24:51

尝试将use行更改为use AppWorld\FrostHeart\baseController;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30067453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档