これらは別のファイルにあると仮定します。
<?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 クラスにはありません。致命的なエラーが発生します。誤字脱字チェックしました。すべて同じディレクトリにあります。Quiz が質問をインポートすると、ファクトリが機能します。しかし、質問が答えになると、致命的なエラーが発生します。クイズは、オートローダーによって呼び出された別のオブジェクト内のオブジェクトであることに注意してください。
私は得る
PHP Fatal error: Class 'aName\Answer' not found in C:\<A PATH>\Question.php on line 315
私は完全に迷っています。ファイルも含まれていますが、クラスを認識しません。名前空間の問題であるとしか推測できませんが、それが何であるかはわかりません。