0
0
PHPprogramming~5 mins

Properties and visibility in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Apublic
Bprivate
Cprotected
Dstatic
If a property is declared as protected, where can it be accessed?
AInside the class and child classes
BOnly inside the class
CAnywhere, including outside the class
DOnly outside the class
What happens if you try to access a private property from outside its class?
AIt causes a fatal error
BIt works normally
CIt returns null
DIt triggers a warning but continues
Which keyword would you use to allow a property to be accessed from anywhere?
Aprivate
Bprotected
Cfinal
Dpublic
Why might you choose private over public for a property?
ATo allow easy access from outside
BTo make the property static
CTo protect the property from unwanted changes
DTo allow access only in child classes
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.