There are three ways to make the data available:
1. Access the $_GET data in the constructor:
class Question {
public function __construct() {
$this->id = isset($_GET['question']) ? $_GET['question'] : 0;
}
}
2. Pass the variable as a constructor parameter:
class Question {
public function __construct($questionId) {
$this->id = $questionID;
}
}
Then pass the value when you initialize the new Question:
$question = new Question($_GET['question']);
3. Use a setter to set the value after the object is created:
class Question {
public function setId($questionId) {
$this->id = $questionId
}
}
Then set the value after you initialize the question:
$question = new Question();
$question->setId($_GET['question']);
Use the first method if you are absolutely sure that the way you get the ID will never change (this is not common).
Use the second method if you are sure that the class will need the ID in every case.
Use the third method if the class may or may not need the ID, or if you need a way to change the ID during the life of the object.