So, I think you are a little foggy on what $this
is for. It is simply a way to refer to the instance of the class that is being utilized. This reference only occurs within the class.
For example:
class Question
{
function __construct($question, $correctAnswer)
{
$this->question = $question;
$this->correctAnswer = $correctAnswer;
}
function answerQuestion($answer)
{
if ($answer == $this->correctAnswer) {
return true;
} else {
return false;
}
}
}
Notice to determine if the answer is correct we compare the provided answer against:
$this->correctAnswer
If we create two different questions:
$questionOne = new Question("Who is the founder of Microsoft?", "Bill Gates");
$questionTwo = new Question("Who is the CEO of Apple, Inc?", "Tim Cook");
And provide the same answer, we get different results:
$isCorrect = $questionOne->answerQuestion("Tim Cook"); // FALSE
$isCorrect = $questionTwo->answerQuestion("Tim Cook"); // TRUE
This is because $this
refers to the instance being used.
So, within the class, you use $this
.
Outside the class, you use the object name. In this case: $questionOne
or $questionTwo
I hope that helps clear things up.