Methods are called on objects using the arrow operator -> with $this representing the current object.
Step 2: Check each option
$this->greet(); is the correct syntax. The other options use invalid syntax.
Final Answer:
$this->greet(); -> Option D
Quick Check:
Method call syntax = $this->method(); [OK]
Hint: 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
3. What will be the output of the following PHP code?
class Person {
public $name = 'Alice';
public function sayName() {
return 'Name: ' . $this->name;
}
}
$p = new Person();
echo $p->sayName();
medium
A. Name: Alice
B. Name: $name
C. Name:
D. Error: Undefined property
Solution
Step 1: Understand property access with $this
The method sayName() returns the string 'Name: ' concatenated with the object's name property accessed via $this->name.
Step 2: Check the property value and output
The property name is set to 'Alice', so the output will be 'Name: Alice'.
Final Answer:
Name: Alice -> Option A
Quick Check:
Property accessed via $this = 'Alice' [OK]
Hint: Remember: $this->property accesses current object's data [OK]
Common Mistakes:
Forgetting to use $this to access properties
Expecting literal string output without variable substitution
Confusing property name with string '$name'
4. Identify the error in this PHP class method:
class Car {
public $model = 'Tesla';
public function getModel() {
return $model;
}
}
medium
A. Missing $this when accessing $model
B. Method name should be GetModel with capital G
C. Property $model must be private
D. Return statement should be return $this->getModel;
Solution
Step 1: Check property access inside method
Inside class methods, properties must be accessed with $this->propertyName. Here, $model is used without $this->.
Step 2: Validate other options
Method names are case-insensitive in PHP, property visibility is not an error here, and the suggested return $this->getModel; is incorrect.
Final Answer:
Missing $this when accessing $model -> Option A
Quick Check:
Use $this->property to access properties [OK]
Hint: Always use $this-> to access object properties inside methods [OK]
Common Mistakes:
Accessing properties without $this->
Confusing method name case sensitivity
Misunderstanding property visibility rules
5. Given the class below, what will be the output?
class Counter {
private $count = 0;
public function increment() {
$this->count++;
}
public function getCount() {
return $this->count;
}
}
$c = new Counter();
$c->increment();
$c->increment();
echo $c->getCount();
hard
A. Error: Cannot access private property
B. 2
C. 1
D. 0
Solution
Step 1: Understand property increment inside the class
The increment() method increases the private $count property by 1 each time it is called.
Step 2: Trace method calls and final output
Calling increment() twice increases $count from 0 to 2. Then getCount() returns 2.
Final Answer:
2 -> Option B
Quick Check:
Two increments = count 2 [OK]
Hint: Private properties accessed via $this inside class work fine [OK]
Common Mistakes:
Assuming private properties can't be accessed inside class methods
Forgetting that increment operator changes value
Expecting output before increments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.