Php 仅在1个特定类上发生命名空间自动包含错误
假设这些文件位于不同的文件中Php 仅在1个特定类上发生命名空间自动包含错误,php,namespaces,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($filen
<?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);
}
}
PHP Fatal error: Class 'aName\Answer' not found in C:\<A PATH>\Question.php on line 315