NameSpace仅在1个特定类上自动包含错误

时间:2012-08-23 22:22:17

标签: php namespaces

假设这些文件属于单独的文件。

<?php
namespace aName;

function importerLoader($class)
{
  $elements =  explode('\\',$class);
  if (count($elements) == 2 && $elements[0] == __NAMESPACE__)
  {
    $filename = $elements[1].'.php';
    if (file_exists($filename) )
    {
      error_log("calling".$filename);
      require($filename);

    }
    else
    {
      error_log("File does not exist ".$filename);
      return false;
    }
  }
}

spl_autoload_register(__NAMESPACE__.'\importerLoader');

<?php
namespace aName;

class Question
{

    ...
    protected function importAnswers()
    {
      foreach($this->QuestionData as $answer)
      {
        $this->answerObjects[$index] = new Answer($answer);//Fatal error
        also tried
$this->answerObjects[$index] = new Answer::answerFactory($answer);//Fatal error

      }
    }

    ...
    public static function questionFactory($question)
    {
      $returnMe = false;
      if (self::validQuestionType($question))
      {
        switch ($question[0]->qtid)
        {
          case 1:
            $returnMe = new QuestionType1($question);
            break;
          case 2:
            $returnMe = new QuestionType2($question);
            break;
        }
        return $returnMe;
      }
    }

}

<?php
namespace aName;

class Answer
{
    ...
    public static function answerFactory($answer)
    {
      $returnMe = false;

      switch ($answer->qtid)
      {
        case 1:
          $returnMe = new AnswerType1($answer);
          break;
        case 2:
          $returnMe = new AnswerType2($answer);
          break;
        default:
          $returnMe = new Answer($answer);
          break;
      }
      return $returnMe;
    }
}

最后

<?php
namespace aName;

class Quiz
{
    ...

    function importQuestion($question)
    {
      //insert newly created into it's proper ordering int he question objects table.
      $this->_questionObjects[$question[0]->question_order] = Question::questionFactory($question);

    }
}

注意:自动加载器适用于所有其他类。 Answer.php类没有。我收到致命错误。我检查了错别字。它都在同一个目录中。 当测验导入工厂的问题时。但是当问题得到答案时我会得到一个致命的错误。 请注意,测验是另一个由自动加载器调用的对象中的对象。

我得到了

PHP Fatal error:  Class 'aName\Answer' not found in C:\<A PATH>\Question.php on line 315

我彻底迷失了。它甚至包括文件,但它不识别该类。我只能猜测它是一个命名空间问题,但我无法弄清楚它是什么。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。我不知道为什么,但编辑器正在以不正确的编码保存该文件。交换它,它现在有效。

相关问题