Properties store information inside an object. Visibility controls who can see or change these properties.
Properties and visibility in PHP
<?php class ClassName { public $property1; // visible everywhere protected $property2; // visible in this class and subclasses private $property3; // visible only inside this class } ?>
public means anyone can access the property.
protected means only this class and classes that extend it can access the property.
private means only this class can access the property.
<?php class Car { public $color; protected $engineNumber; private $ownerName; }
<?php $car = new Car(); $car->color = 'red'; // works because color is public // $car->engineNumber = '123'; // error: protected // $car->ownerName = 'Alice'; // error: private ?>
<?php class Bike extends Car { public function showEngine() { return $this->engineNumber; // allowed because protected } }
This program shows how public, protected, and private properties behave. We access protected and private properties using public methods.
<?php class Person { public $name; protected $age; private $salary; public function __construct($name, $age, $salary) { $this->name = $name; $this->age = $age; $this->salary = $salary; } public function getAge() { return $this->age; } public function getSalary() { return $this->salary; } } $person = new Person('John', 30, 50000); echo $person->name . "\n"; // public, accessible // echo $person->age; // error: protected // echo $person->salary; // error: private echo $person->getAge() . "\n"; // access protected via method echo $person->getSalary() . "\n"; // access private via method ?>
Use public for properties you want everyone to access.
Use protected to hide properties but allow child classes to use them.
Use private to keep properties hidden inside the class only.
Properties hold data inside objects.
Visibility controls who can see or change properties: public, protected, private.
Use methods to access protected or private properties safely.
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
