Skip to main content
make title about the code itself, not the objectives of code review; update grammar- make tenses work
Source Link

please check SOLID principles in my Questionnaire php scriptsclasses and interfaces

I Need your help, please  . I'm trying to develop a simple Questionnaire.

hereHere is what I did already. First, first I have interfaces for Question and Answer entityentities.

Then I have classes which implementsimplement these interfaces. Both Answers and Questions hashave translations in different languages - that's why each of them has $current_languagehave a $current_language property.

And here is a class which does all jobjobs related with questions and answers.

What I 'm concerned about is that I have code duplication in the Question and Answer classes - I mean setCurrentLanguagesetCurrentLanguage and getCurrentLanguagegetCurrentLanguage methods maybe. Maybe it's better to move them into some abstract class and then inheritinherit both Answer and Question classes from it ? Are SOLID principles being violated here?

please check SOLID principles in my Questionnaire php scripts

Need your help, please  . I'm trying to develop simple Questionnaire

here is what I did already, first I have interfaces for Question and Answer entity

Then I have classes which implements these interfaces. Both Answers and Questions has translations in different languages that's why each of them has $current_language property.

And here is class which does all job related with questions and answers.

What I 'm concerned about is that I have code duplication in Question and Answer classes I mean setCurrentLanguage and getCurrentLanguage methods maybe it's better to move them into some abstract class and then inherit both Answer and Question classes from it ?

Questionnaire classes and interfaces

I Need your help, please. I'm trying to develop a simple Questionnaire.

Here is what I did already. First, I have interfaces for Question and Answer entities.

Then I have classes which implement these interfaces. Both Answers and Questions have translations in different languages - that's why each of them have a $current_language property.

And here is a class which does all jobs related with questions and answers.

What I 'm concerned about is that I have code duplication in the Question and Answer classes - I mean setCurrentLanguage and getCurrentLanguage methods. Maybe it's better to move them into some abstract class and then inherit both Answer and Question classes from it ? Are SOLID principles being violated here?

Source Link

please check SOLID principles in my Questionnaire php scripts

Need your help, please . I'm trying to develop simple Questionnaire

here is what I did already, first I have interfaces for Question and Answer entity

interface IAnswerProvider{

    public function getAnswers();

}

interface IQuestionProvider{
    public function getQuestions();
}

Then I have classes which implements these interfaces. Both Answers and Questions has translations in different languages that's why each of them has $current_language property.

class Answer implements IAnswerProvider
{

    private $current_language = '';

    private $current_question_id = 0;


    public function __construct($current_language, $question_id = 0)
    {
        if (!empty($question_id)) {
            $this->setCurrentQuestionId($question_id);
        }

        $this->setCurrentLanguage($current_language);
    }

    public function getAnswers()
    {

        $question_id = $this->getCurrentQuestionId();

        if (empty($question_id)) {
            throw new Error('Question ID is not defined');
        }

        $query = 'select * from  answers a   where a.question_id=' . escape($question_id);

        return DB::getInstance()->fetchAllRows($query);
    }


    /**
     * @return string
     */
    public function getCurrentLanguage()
    {
        return $this->current_language;
    }

    /**
     * @param string $current_language
     */
    public function setCurrentLanguage($current_language)
    {
        $this->current_language = $current_language;
    }

    /**
     * @return int
     */
    public function getCurrentQuestionId()
    {
        return $this->current_question_id;
    }

    /**
     * @param int $current_question_id
     */
    public function setCurrentQuestionId($current_question_id)
    {
        $this->current_question_id = $current_question_id;
    }

}

class Question implements IQuestionProvider
{

    private $current_language = '';

    public function __construct($current_language)
    {
        $this->setCurrentLanguage($current_language);
    }

    /**
     * @return string
     */
    public function getCurrentLanguage()
    {
        return $this->current_language;
    }

    /**
     * @param string $current_languge
     */
    public function setCurrentLanguage($current_language)
    {
        $this->current_language = $current_language;
    }




    public function getQuestions()
    {


        $query = 'select * from questions q where language ='.escape($this->getCurrentLanguage());


        return DB::getInstance()->fetchAllRows($query);
    }


}

And here is class which does all job related with questions and answers.

class Questionnaire{

    private $oQuestionProvider = '';

    private $oAnswerProvider = '';

    public function __construct(IQuestionProvider $oQueston, IAnswerProvider $oAnswer)
    {
        $this->oQuestionProvider = $oQueston;
        $this->oAnswerProvider = $oAnswer;
    }

    /**
    * Returns an array of questions , each question has answers property 
    * with array of answers
    */
    public function getQuestionsWithAnswers(){

        $aData = $this->oQuestionProvider->getQuestions();

        //assign answers to each question
        foreach ($aData as $v){
            $this->oAnswerProvider->setCurrentQuestionId($v->question_id);
            $v->answers = $this->oAnswerProvider->getAnswers();
        }

        return $aData;
    }

}

What I 'm concerned about is that I have code duplication in Question and Answer classes I mean setCurrentLanguage and getCurrentLanguage methods maybe it's better to move them into some abstract class and then inherit both Answer and Question classes from it ?

Please let me know what do you think about this code. Thanks