Bird
0
0

Which of the following is the correct way to call a method named greet inside a PHP class using $this?

easy📝 Syntax Q12 of 15
PHP - Classes and Objects
Which of the following is the correct way to call a method named greet inside a PHP class using $this?
A<code>this->greet();</code>
B<code>self->greet();</code>
C<code>$this.greet();</code>
D<code>$this->greet();</code>
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP method call syntax inside classes

    Methods are called on objects using the arrow operator -> with $this representing the current object.
  2. Step 2: Check each option

    $this->greet(); is the correct syntax. The other options use invalid syntax.
  3. Final Answer:

    $this->greet(); -> Option D
  4. Quick Check:

    Method call syntax = $this->method(); [OK]
Quick Trick: Use $this->methodName(); to call methods inside class [OK]
Common Mistakes:
  • Using dot (.) instead of arrow (->) operator
  • Omitting the dollar sign in $this
  • Using self without scope resolution operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes