Access modifiers (public, private, protected) in PHP - Time & Space Complexity
When using access modifiers in PHP, it's helpful to understand how they affect the way code runs.
We want to see how the choice of public, private, or protected changes the number of steps the program takes.
Analyze the time complexity of the following code snippet.
class Example {
private array $data;
public function __construct(array $items) {
$this->data = $items;
}
public function getItem(int $index) {
return $this->data[$index];
}
}
$example = new Example(range(1, 1000));
echo $example->getItem(500);
This code stores a list of items privately and allows public access to get one item by index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing an array element by index.
- How many times: Each call to
getItemaccesses one element directly, no loops involved.
Accessing an item by index takes the same steps no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of steps stays the same even if the list grows larger.
Time Complexity: O(1)
This means accessing data with these modifiers takes a constant amount of time regardless of the data size.
[X] Wrong: "Using private or protected makes data access slower because it restricts visibility."
[OK] Correct: Access modifiers control who can see or change data, but they do not add extra steps when accessing data inside the class.
Understanding how access modifiers affect code helps you write clear and efficient programs, a skill valued in many coding challenges and real projects.
"What if the getItem method used a loop to find the item instead of direct access? How would the time complexity change?"
Practice
Solution
Step 1: Understand access levels
Public means accessible from anywhere, private means only inside the class, protected means inside class and subclasses.Step 2: Match the description
The question asks for access from anywhere, which matches public.Final Answer:
public -> Option AQuick Check:
Public = accessible anywhere [OK]
- Confusing private with public access
- Thinking protected is accessible outside class
- Mixing static with access modifiers
$data in a PHP class?Solution
Step 1: Recall PHP property declaration syntax
Access modifiers come before the variable name with a dollar sign, e.g., private $data;Step 2: Identify private declaration
Only private $data; uses 'private' correctly before the variable.Final Answer:
private $data; -> Option AQuick Check:
private property syntax = private $data; [OK]
- Using 'var' instead of access modifiers
- Forgetting the dollar sign before variable
- Mixing public or protected when private is needed
class Test {
private $secret = 'hidden';
public function reveal() {
return $this->secret;
}
}
$obj = new Test();
echo $obj->reveal();Solution
Step 1: Understand private property access inside class
Private properties can be accessed inside class methods using $this->property.Step 2: Check method reveal() returns private property
The reveal() method returns $this->secret, which is 'hidden'. So echo prints 'hidden'.Final Answer:
hidden -> Option DQuick Check:
Private accessed inside class method = allowed [OK]
- Thinking private properties can't be accessed even inside class
- Expecting error when accessing private inside class
- Confusing property name with string output
class ParentClass {
protected $value = 10;
}
class ChildClass extends ParentClass {
public function getValue() {
return $this->value;
}
}
$obj = new ParentClass();
echo $obj->value;Solution
Step 1: Understand protected property access
Protected properties can be accessed inside the class and child classes, but not from outside.Step 2: Check code usage
The code tries to echo $obj->value where $obj is ParentClass instance, accessing protected property from outside, causing error.Final Answer:
Cannot access protected property from outside the class -> Option CQuick Check:
Protected outside class = error [OK]
- Trying to access protected property directly from object
- Confusing protected with public
- Assuming no error when accessing protected externally
class Base {
private $data = 'base';
protected $info = 'protected info';
public function getData() {
return $this->data;
}
}
class Derived extends Base {
public function getInfo() {
return $this->info;
}
public function getDataFromBase() {
return $this->data;
}
}
$obj = new Derived();
echo $obj->getData() . ' & ' . $obj->getInfo() . ' & ' . $obj->getDataFromBase();Solution
Step 1: Analyze private property access in Derived
Private property $data is only accessible inside Base class. Derived cannot access $this->data directly.Step 2: Check method calls and outputs
getData() returns $data from Base (allowed), getInfo() returns protected $info (allowed in Derived), getDataFromBase() tries to access private $data directly (causes error).Final Answer:
base & protected info & Error accessing private property -> Option BQuick Check:
Private inaccessible in child = error [OK]
- Assuming private properties are inherited and accessible
- Confusing protected with private access
- Expecting no error when accessing private from child
Ready to prove it?
Pass the test and this pattern moves unknown
