0
0
PHPprogramming~10 mins

__get and __set for property access in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - __get and __set for property access
Access property obj->prop
Is property accessible?
YesReturn property value
No
Call __get('prop')
Return value from __get
Assign obj->prop = value
Is property accessible?
YesSet property value
No
Call __set('prop', value)
Store value in __set
When you try to get or set a property that is not accessible, PHP calls __get or __set methods to handle it.
Execution Sample
PHP
<?php
class Person {
  private $data = [];
  public function __get($name) {
    return $this->data[$name] ?? 'Not set';
  }
  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
}
$p = new Person();
$p->age = 30;
echo $p->age;
echo $p->name;
?>
This code sets and gets a property 'age' using __set and __get because 'age' is not a declared property.
Execution Table
StepActionPropertyValueMethod CalledResult/Output
1Assign propertyage30__setStores 30 in data['age']
2Access propertyage__getReturns 30
3Output30
4Access undefined propertyname__getReturns 'Not set'
5OutputNot set
💡 No more code to execute, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
data[]{"age":30}{"age":30}{"age":30}{"age":30}
ageundefined30303030
nameundefinedundefinedundefinedundefinedundefined
Key Moments - 3 Insights
Why does PHP call __set when assigning to $p->age?
Because 'age' is not a declared property, PHP calls __set to handle the assignment, as shown in execution_table step 1.
Why does accessing $p->age call __get instead of returning a property directly?
Since 'age' is not declared, PHP calls __get to retrieve the value, as shown in execution_table step 2.
What happens if you access a property that was never set?
The __get method returns 'Not set' as a default, shown in execution_table step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what method is called when assigning $p->age = 30?
A__set
B__get
CNo method, direct assignment
D__construct
💡 Hint
Check execution_table step 1 where assignment triggers __set.
At which step does accessing an undefined property return 'Not set'?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
See execution_table step 4 where __get returns 'Not set'.
If the property 'age' was declared public, what would happen when assigning $p->age = 30?
A__set would still be called
BDirect assignment without __set
C__get would be called
DError occurs
💡 Hint
Refer to concept_flow: __set is called only if property is inaccessible.
Concept Snapshot
__get and __set are magic methods in PHP.
They run when accessing or setting inaccessible properties.
__get($name) returns a value for $obj->$name.
__set($name, $value) sets a value for $obj->$name.
Use them to control dynamic property access safely.
Full Transcript
This example shows how PHP uses __get and __set when you try to get or set a property that is not declared or accessible. When you assign a value to such a property, __set is called to handle storing the value. When you read such a property, __get is called to return a value. If the property was never set, __get can return a default like 'Not set'. This lets you control how your object handles properties dynamically.