assume these are in seperate files.
<?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;
}
}
and finally
<?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);
}
}
Note: the autoloader works for ALL other classes. The Answer.php class does not. I get a Fatal Error. I have checked typos. it is all in the same directory. When Quiz imports a question the factory works. but when the question makes an answer i get a fatal error. Note that the quiz is an object in another object that was called byt he autoloader.
I get
PHP Fatal error: Class 'aName\Answer' not found in C:\<A PATH>\Question.php on line 315
i am thoroughly lost. it even includes the file but it does not recognize the class. I can only guess it is a namespacing issue but I cannot figure out what it is.
I discovered the issue. I don't know why but the editor was saving that file at the incorrect encoding. swapped it and it works now.