0
0
PHPprogramming~10 mins

Access modifiers (public, private, protected) in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Access modifiers (public, private, protected)
Define Class with Members
Access Modifier Check
Public
Accessible Anywhere
Access Attempt
Allowed or Error
This flow shows how PHP checks access modifiers when you try to use class members: public is open everywhere, protected is limited to class and subclasses, private only inside the class.
Execution Sample
PHP
<?php
class Box {
  public $width = 10;
  private $height = 20;
  protected $depth = 30;
}
$box = new Box();
echo $box->width;
echo $box->height; // error
?>
This code defines a class with public, private, and protected properties and tries to access them from outside the class.
Execution Table
StepActionMember AccessAccess ModifierAllowed?Output / Result
1Create object $box--YesObject created
2Access $box->widthwidthpublicYes10
3Access $box->heightheightprivateNoFatal error: Cannot access private property Box::$height
4Access $box->depthdepthprotectedNoFatal error: Cannot access protected property Box::$depth
💡 Execution stops at step 3 due to fatal error accessing private property from outside class.
Variable Tracker
VariableStartAfter Object CreationFinal
$box->widthundefined1010
$box->heightundefined2020
$box->depthundefined3030
Key Moments - 2 Insights
Why can I access the public property but get an error with private or protected?
Public properties are open to all code (see step 2 in execution_table). Private and protected restrict access: private only inside the class (step 3 error), protected inside class and subclasses (step 4 error).
What is the difference between private and protected?
Private means only the class itself can access the member. Protected allows access inside the class and any subclass. Both block outside access (see steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens when accessing $box->depth from outside the class?
AIt outputs nothing
BIt outputs 30
CIt causes a fatal error
DIt returns null
💡 Hint
Check step 4 in the execution_table where $box->depth is accessed.
At which step does the program stop due to an error?
AStep 3
BStep 2
CStep 4
DAfter all steps
💡 Hint
Look at the exit_note and step 3 in execution_table.
If $height was declared public instead of private, what would change in the execution_table?
AStep 4 would cause an error instead
BStep 3 would allow access and output 20
CStep 3 would still cause an error
DNo change at all
💡 Hint
Think about how public access works from the concept_flow and execution_table step 2.
Concept Snapshot
Access modifiers control visibility of class members:
- public: accessible anywhere
- protected: accessible in class and subclasses
- private: accessible only inside the class
Trying to access private or protected from outside causes errors.
Use modifiers to protect data and control access.
Full Transcript
This visual execution shows how PHP access modifiers work. We define a class with public, private, and protected properties. When we create an object and try to access these properties from outside, public works fine, but private and protected cause errors. Public means open to all code. Private means only the class itself can access. Protected means the class and its subclasses can access. Trying to access private or protected from outside the class causes fatal errors. This helps protect data inside objects and control how other code interacts with them.