Recall & Review
beginner
What does the public access modifier mean in PHP?
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?
✗ Incorrect
The public modifier allows access from anywhere.
Which access modifier restricts access only to the class where the property or method is declared?
✗ Incorrect
Private restricts access only to the declaring class.
Can a protected property be accessed from a subclass?
✗ Incorrect
Protected properties can be accessed from subclasses.
What happens if you try to access a private property from outside its class?
✗ Incorrect
Accessing private properties outside the class causes an error.
Which access modifier is best to hide sensitive data inside a class?
✗ 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.