Bird
0
0

You want to create a class Student with a property name and a method greet() that returns "Hello, " plus the student's name. Which code correctly implements this?

hard📝 Application Q8 of 15
PHP - Classes and Objects
You want to create a class Student with a property name and a method greet() that returns "Hello, " plus the student's name. Which code correctly implements this?
Aclass Student { public $name; public function greet() { return "Hello, " . $this->name; } }
Bclass Student { public $name; function greet() { return "Hello, " + $name; } }
Cclass Student { public $name; public function greet() { return "Hello, " . name; } }
Dclass Student { public $name; public greet() { return "Hello, " . $this->name; } }
Step-by-Step Solution
Solution:
  1. Step 1: Check method syntax and property access

    Method must be declared with public function and access property with $this->name.
  2. Step 2: Verify string concatenation

    PHP uses . to concatenate strings, so "Hello, " . $this->name is correct.
  3. Final Answer:

    class Student { public $name; public function greet() { return "Hello, " . $this->name; } } -> Option A
  4. Quick Check:

    Use public function and $this->property with . for concat [OK]
Quick Trick: Use $this->property and '.' to concatenate strings [OK]
Common Mistakes:
  • Using + for string concat
  • Omitting 'function' keyword
  • Accessing property without $this->

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes