Bird
0
0

Which of the following is the correct syntax to declare a protected property named $age in a PHP class?

easy📝 Syntax Q3 of 15
PHP - Classes and Objects
Which of the following is the correct syntax to declare a protected property named $age in a PHP class?
Avar $age;
Bpublic $age;
Cprotected $age;
Dprivate age;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct visibility keyword and syntax

    To declare a protected property, use the keyword 'protected' followed by the property name with a $ sign.
  2. Step 2: Check each option for syntax correctness

    protected $age; uses 'protected $age;' which is correct. public $age; is public, not protected. private age; misses the $ sign. var $age; uses 'var' which is deprecated for visibility.
  3. Final Answer:

    protected $age; -> Option C
  4. Quick Check:

    Protected property syntax = protected $property [OK]
Quick Trick: Use 'protected $name;' to declare protected properties [OK]
Common Mistakes:
  • Omitting the $ sign
  • Using 'var' instead of visibility keywords
  • Confusing public and protected

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes