Programming Languages PHP Subjective
Sep 18, 2025

Explain visibility modifiers in PHP classes.

Detailed Explanation
public: Accessible from anywhere\nprivate: Only accessible within the same class\nprotected: Accessible within the class and its subclasses\n\nExample:\nclass MyClass {\n public $publicVar = "Everyone can see this";\n private $privateVar = "Only MyClass can see this";\n protected $protectedVar = "MyClass and subclasses can see this";\n}\n\nclass ChildClass extends MyClass {\n public function test() {\n echo $this->publicVar; // OK\n echo $this->protectedVar; // OK\n echo $this->privateVar; // Error!\n }\n}
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback