Bird
0
0

Which of the following is the correct way to declare a private property named $name in a PHP class?

easy📝 Syntax Q12 of 15
PHP - Classes and Objects
Which of the following is the correct way to declare a private property named $name in a PHP class?
Aprivate $name;
Bvar $name;
Cpublic $name;
Dprotected $name;
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP property declaration syntax

    Properties are declared with visibility keyword followed by variable name and semicolon.
  2. Step 2: Identify private property syntax

    Correct syntax is private $name;. The var keyword is outdated and defaults to public.
  3. Final Answer:

    private $name; -> Option A
  4. Quick Check:

    private property declaration uses 'private' keyword [OK]
Quick Trick: Use 'private' keyword before $property for private visibility [OK]
Common Mistakes:
  • Using 'var' instead of visibility keywords
  • Omitting the $ sign
  • Using public or protected instead of private

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes