📖 Scenario: You are creating a simple PHP class to represent a bank account. You want to control how the account's balance can be accessed and changed to keep it safe.
🎯 Goal: Build a PHP class called BankAccount with public, private, and protected properties and methods to understand how access modifiers work.
📋 What You'll Learn
Create a class named BankAccount
Add a public property called owner with the owner's name
Add a private property called balance with the initial amount
Add a protected method called formatBalance that returns the balance formatted as a string with a dollar sign
Add a public method called getBalance that uses formatBalance to return the formatted balance
Add a public method called deposit that adds money to the balance
💡 Why This Matters
🌍 Real World
Access modifiers help protect sensitive data in real applications like bank accounts, user profiles, or settings.
💼 Career
Understanding access modifiers is essential for writing secure and maintainable object-oriented code in PHP development jobs.
Progress0 / 4 steps
1
Create the BankAccount class with properties
Create a class called BankAccount with a public property owner set to "Alice" and a private property balance set to 1000.
PHP
Hint
Use public and private keywords before the property names inside the class.
2
Add a protected method to format the balance
Inside the BankAccount class, add a protected method called formatBalance that returns the balance formatted as a string with a dollar sign, like "$1000".
PHP
Hint
Use protected function formatBalance() and return the balance with a dollar sign.
3
Add public methods to get balance and deposit money
Add two public methods inside BankAccount: getBalance that returns the result of formatBalance(), and deposit that takes a float $amount and adds it to balance.
PHP
Hint
Use public function getBalance() to call formatBalance() and public function deposit(float $amount) to add to balance.
4
Create an object, deposit money, and print the balance
Create a BankAccount object called account. Call deposit(500) on it. Then print the owner's name and the balance using getBalance().
PHP
Hint
Create the object with new BankAccount(), call deposit(500), then print the owner's name and balance.
Practice
(1/5)
1. Which access modifier allows a property or method to be accessed from anywhere, including outside the class?
easy
A. public
B. private
C. protected
D. static
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 A
Quick Check:
Public = accessible anywhere [OK]
Hint: Public means open to all code everywhere [OK]
Common Mistakes:
Confusing private with public access
Thinking protected is accessible outside class
Mixing static with access modifiers
2. Which of the following is the correct way to declare a private property named $data in a PHP class?
easy
A. private $data;
B. public $data;
C. protected $data;
D. var $data;
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 A
Quick Check:
private property syntax = private $data; [OK]
Hint: Use 'private' keyword before $variable for private property [OK]
Common Mistakes:
Using 'var' instead of access modifiers
Forgetting the dollar sign before variable
Mixing public or protected when private is needed
3. What will be the output of the following PHP code?
class Test {
private $secret = 'hidden';
public function reveal() {
return $this->secret;
}
}
$obj = new Test();
echo $obj->reveal();
medium
A. secret
B. Error: Cannot access private property
C. null
D. hidden
Solution
Step 1: Understand private property access inside class
Private properties can be accessed inside class methods using $this->property.