Recall & Review
beginner
What are the three visibility keywords for properties in PHP classes?
The three visibility keywords are <strong>public</strong>, <strong>protected</strong>, and <strong>private</strong>. They control access to class properties.Click to reveal answer
beginner
What does a
public property mean in PHP?A
public property can be accessed from anywhere: inside the class, in child classes, and from outside the class.Click to reveal answer
intermediate
Explain the difference between
protected and private properties.<code>Protected</code> properties can be accessed inside the class and by child classes. <code>Private</code> properties can only be accessed inside the class where they are declared.Click to reveal answer
beginner
How do you declare a private property named
$age in a PHP class?You declare it like this:
private $age;
Click to reveal answer
intermediate
Why is property visibility important in object-oriented programming?
Visibility controls how properties can be accessed, helping to protect data and enforce encapsulation. It prevents unwanted changes from outside the class.
Click to reveal answer
Which visibility keyword allows a property to be accessed only within the class it is declared?
✗ Incorrect
private properties are accessible only inside the class they are declared in.
If a property is declared as
protected, where can it be accessed?✗ Incorrect
protected properties are accessible inside the class and by classes that extend it.What happens if you try to access a
private property from outside its class?✗ Incorrect
Accessing a
private property from outside its class causes a fatal error in PHP.Which keyword would you use to allow a property to be accessed from anywhere?
✗ Incorrect
public properties can be accessed from anywhere.Why might you choose
private over public for a property?✗ Incorrect
private properties protect data by restricting access to inside the class only.Describe the three types of property visibility in PHP and give an example of when you might use each.
Think about who should be allowed to see or change the property.
You got /4 concepts.
Explain why controlling property visibility is important for maintaining good code structure in PHP classes.
Consider how visibility helps keep code safe and organized.
You got /4 concepts.