2013-01-14 75 views
2

我不知道如何解释它,但我会尽我所能。好吧,我有这三个文件:实例化一个对象到一个名字空间的类

  • Theme.php

    path: /shared/models/Theme.php 
    class: Theme 
    namespace: namespace models; 
    
  • Custom.php

    path: /themes/default/Custom.php 
    class: Custom 
    namespace: this class does not use namespace 
    
  • 的settings.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类而不是用$ext var?

回答

3

当在命名空间的一个类中调用new Custom()时,您实际上试图实例化\models\Custom。既然你说你的Custom类“没有命名空间”,请尝试使用new \Custom()

您收到的错误似乎来自某些尝试为\models\Custom要求类文件并失败的类自动加载器。

+0

是的,你是完全正确的。记住这一点非常重要:'因为你说你的自定义类没有名字空间,所以试试新的\ Custom()。“谢谢你:) – manix

相关问题