Bird
0
0

Why is it recommended to use private or protected access modifiers instead of public for class properties in PHP?

hard📝 Conceptual Q10 of 15
PHP - Classes and Objects
Why is it recommended to use private or protected access modifiers instead of public for class properties in PHP?
ATo enforce encapsulation and control access to internal data
BBecause public properties cause syntax errors
CTo make properties accessible only from outside the class
DBecause private and protected properties run faster
Step-by-Step Solution
Solution:
  1. Step 1: Understand encapsulation principle

    Encapsulation means hiding internal details and controlling access to data.
  2. Step 2: Role of access modifiers

    Private and protected restrict access, helping maintain integrity and prevent unintended changes.
  3. Final Answer:

    To enforce encapsulation and control access to internal data -> Option A
  4. Quick Check:

    Encapsulation = use private/protected [OK]
Quick Trick: Use private/protected to protect data inside classes [OK]
Common Mistakes:
  • Thinking public causes errors
  • Believing private/protected improve speed
  • Confusing access direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes