Skip to main content

What is the difference between public, private, and protected?

Junior PHP
Quick Answer public: accessible from anywhere (outside the class, subclasses, same class). protected: accessible within the class and its subclasses. private: accessible only within the same class - not even subclasses. Apply to properties and methods. Default is public in PHP. Use private for internal implementation, protected for extensible behavior, public for the class's interface.

Answer

public is accessible everywhere, private only inside the class, protected inside the class and subclasses.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice