The public access modifier means the property or method can be accessed from anywhere: inside the class, from subclasses, and from outside the class.
Click to reveal answer
beginner
What is the difference between private and protected access modifiers?
<strong>Private</strong> means the property or method can only be accessed inside the class where it is declared. <strong>Protected</strong> means it can be accessed inside the class and by subclasses (child classes), but not from outside.
Click to reveal answer
beginner
Show a simple PHP class with one public, one private, and one protected property.
<?php
class Example {
public $name;
private $age;
protected $email;
}
?>
Click to reveal answer
beginner
Why do we use access modifiers in PHP classes?
Access modifiers help control who can see or change properties and methods. This protects data and helps organize code by hiding details that should not be accessed directly.
Click to reveal answer
beginner
Can a subclass access a private property of its parent class directly?
No, a subclass cannot access a private property of its parent class directly. Private properties are only accessible inside the class where they are declared.
Click to reveal answer
Which access modifier allows access from anywhere, including outside the class?
Apublic
Bprivate
Cprotected
Dstatic
✗ Incorrect
The public modifier allows access from anywhere.
Which access modifier restricts access only to the class where the property or method is declared?
Apublic
Bprivate
Cprotected
Dfinal
✗ Incorrect
Private restricts access only to the declaring class.
Can a protected property be accessed from a subclass?
AOnly if public
BNo
CYes
DOnly if static
✗ Incorrect
Protected properties can be accessed from subclasses.
What happens if you try to access a private property from outside its class?
AIt converts to public
BIt works normally
CIt returns null
DIt causes an error
✗ Incorrect
Accessing private properties outside the class causes an error.
Which access modifier is best to hide sensitive data inside a class?
Aprivate
Bpublic
Cprotected
Dglobal
✗ Incorrect
Private is best to hide sensitive data inside a class.
Explain the differences between public, private, and protected access modifiers in PHP.
Think about who can see or use the property or method.
You got /3 concepts.
Why is it important to use access modifiers when writing PHP classes?
Consider how access modifiers help keep code safe and clear.
You got /3 concepts.
Practice
(1/5)
1. Which access modifier allows a property or method to be accessed from anywhere, including outside the class?
easy
A. public
B. private
C. protected
D. static
Solution
Step 1: Understand access levels
Public means accessible from anywhere, private means only inside the class, protected means inside class and subclasses.
Step 2: Match the description
The question asks for access from anywhere, which matches public.
Final Answer:
public -> Option A
Quick Check:
Public = accessible anywhere [OK]
Hint: Public means open to all code everywhere [OK]
Common Mistakes:
Confusing private with public access
Thinking protected is accessible outside class
Mixing static with access modifiers
2. Which of the following is the correct way to declare a private property named $data in a PHP class?
easy
A. private $data;
B. public $data;
C. protected $data;
D. var $data;
Solution
Step 1: Recall PHP property declaration syntax
Access modifiers come before the variable name with a dollar sign, e.g., private $data;
Step 2: Identify private declaration
Only private $data; uses 'private' correctly before the variable.
Final Answer:
private $data; -> Option A
Quick Check:
private property syntax = private $data; [OK]
Hint: Use 'private' keyword before $variable for private property [OK]
Common Mistakes:
Using 'var' instead of access modifiers
Forgetting the dollar sign before variable
Mixing public or protected when private is needed
3. What will be the output of the following PHP code?
class Test {
private $secret = 'hidden';
public function reveal() {
return $this->secret;
}
}
$obj = new Test();
echo $obj->reveal();
medium
A. secret
B. Error: Cannot access private property
C. null
D. hidden
Solution
Step 1: Understand private property access inside class
Private properties can be accessed inside class methods using $this->property.