I am fairly new to php. I have classes called quiz and question. And the class quiz can have one or many questions. I have done a java representation
我对php很新。我有一些叫做测验和问题的课程。课堂测验可以有一个或多个问题。我做了一个java表示
class Question () {
private int id;
private String question;
private String answer;
}
and the quiz class should be as follows,
测验课应该如下,
class Quiz() {
private int id;
private List<Question> questionList;
}
my question is regarding how to represent the above java representation in php. please be kind enough to point me in the right direction.
我的问题是如何在php中表示上面的java表示。请善意指出我正确的方向。
2 个解决方案
#1
1
getter and setter:
吸气剂和二传手:
class Question {
private $id ;
private $question;
private $answer;
public function __construct(){
}
public function setId($value){
$this->id = $value;
}
public function setQuestion($value){
$this->question = $value;
}
public function setAnswer($value){
$this->answer = $value;
}
public function getId(){
return $this->id;
}
public function getQuestion(){
return $this->question;
}
public function getAnswer(){
return $this->answer;
}
}
class Quiz{
private $id;
private $questionList = array();
public function __construct(){
}
public function setQuestionList($value){
$this->questionList[] = $value;
}
public function getQuestionList(){
return $this->questionList;
}
}
//
$quiz = new Quiz();
//
$question = new Question();
$question->setId(1);
$question->setQuestion('question?');
$question->setAnswer('answer');
//
$quiz->setQuestionList($question);
//
$question = new Question();
$question->setId(2);
$question->setQuestion('question2?');
$question->setAnswer('answer2');
//
$quiz->setQuestionList($question);
//
//Getting questions
foreach($quiz->getQuestionList() as $object){
echo $object->getId().' - '.$object->getQuestion().' - '.$object->getAnswer().'<br />';
}
#2
0
Generics type don't exist in standard PHP.
标准PHP中不存在泛型类型。
One way to represent such structure in PHP is :
在PHP中表示此类结构的一种方法是:
class Question
{
private $id;
private $question;
private $answer;
}
class Quiz
{
private $id;
/**
* @var array<Question>
*/
private $questionList = array();
}
The docblock (@var
...) is just here to help others (and IDEs) to understand the structure of $questionList
.
docblock(@var ...)就是为了帮助其他人(和IDE)理解$ questionList的结构。
To add some check on $questionList
content, you can use typed setter/adder :
要在$ questionList内容上添加一些检查,您可以使用typed setter / adder:
public function addQuestion(Question $question)
{
$this->questionList[] = $question;
}
#1
1
getter and setter:
吸气剂和二传手:
class Question {
private $id ;
private $question;
private $answer;
public function __construct(){
}
public function setId($value){
$this->id = $value;
}
public function setQuestion($value){
$this->question = $value;
}
public function setAnswer($value){
$this->answer = $value;
}
public function getId(){
return $this->id;
}
public function getQuestion(){
return $this->question;
}
public function getAnswer(){
return $this->answer;
}
}
class Quiz{
private $id;
private $questionList = array();
public function __construct(){
}
public function setQuestionList($value){
$this->questionList[] = $value;
}
public function getQuestionList(){
return $this->questionList;
}
}
//
$quiz = new Quiz();
//
$question = new Question();
$question->setId(1);
$question->setQuestion('question?');
$question->setAnswer('answer');
//
$quiz->setQuestionList($question);
//
$question = new Question();
$question->setId(2);
$question->setQuestion('question2?');
$question->setAnswer('answer2');
//
$quiz->setQuestionList($question);
//
//Getting questions
foreach($quiz->getQuestionList() as $object){
echo $object->getId().' - '.$object->getQuestion().' - '.$object->getAnswer().'<br />';
}
#2
0
Generics type don't exist in standard PHP.
标准PHP中不存在泛型类型。
One way to represent such structure in PHP is :
在PHP中表示此类结构的一种方法是:
class Question
{
private $id;
private $question;
private $answer;
}
class Quiz
{
private $id;
/**
* @var array<Question>
*/
private $questionList = array();
}
The docblock (@var
...) is just here to help others (and IDEs) to understand the structure of $questionList
.
docblock(@var ...)就是为了帮助其他人(和IDE)理解$ questionList的结构。
To add some check on $questionList
content, you can use typed setter/adder :
要在$ questionList内容上添加一些检查,您可以使用typed setter / adder:
public function addQuestion(Question $question)
{
$this->questionList[] = $question;
}