2

我不知道如何解释,但我会尽力而为。好的,我有这三个文件:

  • 主题.php

    path: /shared/models/Theme.php
    class: Theme
    namespace: namespace models;
    
  • 自定义.php

    path: /themes/default/Custom.php
    class: Custom
    namespace: this class does not use namespace
    
  • 设置.php

    path: /controllers/Settings.php
    class: Settings
    namespace: this class does not use namespace
    

在我Settings.php看来:

<?php
class Settings
{
    public function apply()
    {
        $theme = new \models\Theme();
        $theme->customize(); // in this method the error is triggered
    }
}

现在,看Theme下面的类:

<?php
namespace models;

class Theme
{
    public function customize()
    {
        $ext = "/themes/default/Custom.php";
        if (file_exists($ext))
        {
            include $ext;
            if (class_exists('Custom'))
            {            
                $custom = new Custom(); 
                //Here, $custom var in null, why???
            }
        }
    }
}

当我执行代码时,我收到以下错误:

Message: require(/shared/models/Custom.php) [function.require]: failed to open stream: No such file or directory
Line Number: 64

为什么解释器试图Custom从另一个目录加载类而不是用$extvar 指定?

4

1 回答 1

3

new Custom()在命名空间中的类内部调用时,\models您实际上是在尝试实例化\models\Custom. 既然你说你的Custom班级“没有命名空间”,那就试试吧new \Custom()

您得到的错误似乎来自一些试图要求类文件\models\Custom并失败的类自动加载器。

于 2013-01-14T02:35:57.353 回答