Discover how to keep your code's secrets safe and share only what you want!
Why Properties and visibility in PHP? - Purpose & Use Cases
Imagine you have a big box of toys at home. You want to share some toys with your friends but keep your favorite ones private. Without a way to control access, your friends might grab toys you didn't want to share.
Manually checking and controlling who can play with which toy every time is tiring and confusing. It's easy to make mistakes, like accidentally letting friends play with your private toys or hiding toys so well you forget about them yourself.
Properties and visibility in PHP let you decide which parts of your code (toys) are open to everyone, which are only for close friends (inside the class), and which are private secrets. This keeps your code organized and safe without extra effort.
class ToyBox { public $toy1; public $toy2; } // Anyone can change any toy anytimeclass ToyBox { private $favoriteToy; public function getFavoriteToy() { return $this->favoriteToy; } } // Control who sees or changes toys
It enables you to protect important data and control how your program parts interact, making your code safer and easier to manage.
Think of a bank account: you want anyone to see your balance but only you can change it. Properties and visibility help build this kind of control in your programs.
Properties store data inside objects.
Visibility controls who can see or change these properties.
This keeps your code safe and organized.
Practice
Solution
Step 1: Understand visibility keywords
Public properties can be accessed from anywhere, protected only within the class and subclasses, private only inside the class.Step 2: Identify which keyword allows outside access
Onlypublicallows access from outside the class.Final Answer:
public -> Option BQuick Check:
public = accessible everywhere [OK]
- Confusing protected with public
- Thinking private allows outside access
- Mixing static with visibility
$name in a PHP class?Solution
Step 1: Recall PHP property declaration syntax
Properties are declared with visibility keyword followed by variable name and semicolon.Step 2: Identify private property syntax
Correct syntax isprivate $name;. Thevarkeyword is outdated and defaults to public.Final Answer:
private $name; -> Option AQuick Check:
private property declaration uses 'private' keyword [OK]
- Using 'var' instead of visibility keywords
- Omitting the $ sign
- Using public or protected instead of private
class Car {
private $color = 'red';
public function getColor() {
return $this->color;
}
}
$car = new Car();
echo $car->getColor();Solution
Step 1: Understand private property access
The property$coloris private, so it cannot be accessed directly outside the class.Step 2: Check method usage to access private property
The public methodgetColor()returns the private property value, so calling it outputs 'red'.Final Answer:
red -> Option DQuick Check:
Private property accessed via public method = 'red' [OK]
- Trying to access private property directly
- Expecting an error when using a getter method
- Confusing null with private property value
class Person {
protected $age;
public function setAge($age) {
$this->age = $age;
}
}
$person = new Person();
echo $person->age;Solution
Step 1: Understand protected property visibility
Protected properties cannot be accessed directly from outside the class or its subclasses.Step 2: Analyze code usage of protected property
The code tries to echo$person->agedirectly, which causes an error due to protected visibility.Final Answer:
Cannot access protected property directly outside class -> Option CQuick Check:
Protected = no direct outside access [OK]
- Trying to access protected property directly
- Confusing protected with public
- Ignoring visibility rules
$score only if the new score is higher?class Game {
private $score = 0;
public function updateScore(int $newScore) {
// What code should go here?
}
public function getScore() {
return $this->score;
}
}Solution
Step 1: Understand the requirement to update only if new score is higher
The property$scoreshould change only when$newScoreis greater than current$score.Step 2: Choose the correct conditional update code
if ($newScore > $this->score) { $this->score = $newScore; } checks if$newScoreis greater, then updates$score. Other options either overwrite unconditionally or do wrong comparison.Final Answer:
if ($newScore > $this->score) { $this->score = $newScore; } -> Option AQuick Check:
Update only if newScore > current score [OK]
- Updating score without condition
- Using wrong comparison operator
- Returning boolean instead of updating property
Ready to prove it?
Pass the test and this pattern moves unknown
