Bird
Raised Fist0
PHPprogramming~20 mins

Access modifiers (public, private, protected) in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Access Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code with access modifiers?

Consider this PHP class and code snippet. What will be printed?

PHP
<?php
class Test {
    public $a = 1;
    private $b = 2;
    protected $c = 3;

    public function printValues() {
        echo $this->a . ',' . $this->b . ',' . $this->c;
    }
}

$test = new Test();
echo $test->a . ',';
// echo $test->b . ','; // Uncommenting this line causes error
// echo $test->c; // Uncommenting this line causes error
$test->printValues();
?>
A1,1,2,3
B1,2,3
C1,1,2
D1,1,3
Attempts:
2 left
💡 Hint

Remember that private and protected properties cannot be accessed directly outside the class, but public properties can.

❓ Predict Output
intermediate
1:30remaining
What error occurs when accessing a private property from outside the class?

What error will this PHP code produce?

PHP
<?php
class Sample {
    private $secret = 'hidden';
}

$obj = new Sample();
echo $obj->secret;
?>
ANo output, prints 'hidden'
BParse error: syntax error, unexpected 'private'
CFatal error: Uncaught Error: Cannot access private property Sample::$secret
DNotice: Undefined property: Sample::$secret
Attempts:
2 left
💡 Hint

Private properties cannot be accessed from outside the class.

🔧 Debug
advanced
2:00remaining
Why does this code cause an error when accessing a protected property?

Look at this PHP code. Why does it cause an error?

PHP
<?php
class ParentClass {
    protected $value = 10;
}

class ChildClass extends ParentClass {
}

$obj = new ChildClass();
echo $obj->value;
?>
AProtected properties cannot be accessed directly outside the class or its children
BProtected properties can only be accessed inside the parent class, not children
CThe property <code>$value</code> is not declared public, so it is undefined
DPHP does not support protected properties
Attempts:
2 left
💡 Hint

Think about where protected properties can be accessed from.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a private property and a public method in PHP?

Choose the correct PHP code snippet that declares a private property $data and a public method getData() that returns it.

Aclass Example { private $data; public getData() { return $this->data; } }
Bclass Example { private $data; function getData() { return $data; } }
Cclass Example { public $data; private function getData() { return $this->data; } }
Dclass Example { private $data; public function getData() { return $this->data; } }
Attempts:
2 left
💡 Hint

Remember to use function keyword and $this-> to access properties inside methods.

🚀 Application
expert
2:30remaining
How many properties are accessible inside this child class method?

Given these classes, how many properties can the showProperties() method access?

PHP
<?php
class Base {
    public $pub = 1;
    private $priv = 2;
    protected $prot = 3;
}

class Derived extends Base {
    public function showProperties() {
        $vars = get_object_vars($this);
        $count = 0;
        if (isset($vars['pub'])) $count++;
        if (isset($vars['priv'])) $count++;
        if (isset($vars['prot'])) $count++;
        return $count;
    }
}

$obj = new Derived();
echo $obj->showProperties();
?>
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Think about which properties are accessible inside a child class method.

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

  1. Step 1: Understand access levels

    Public means accessible from anywhere, private means only inside the class, protected means inside class and subclasses.
  2. Step 2: Match the description

    The question asks for access from anywhere, which matches public.
  3. Final Answer:

    public -> Option A
  4. 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

  1. Step 1: Recall PHP property declaration syntax

    Access modifiers come before the variable name with a dollar sign, e.g., private $data;
  2. Step 2: Identify private declaration

    Only private $data; uses 'private' correctly before the variable.
  3. Final Answer:

    private $data; -> Option A
  4. 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

  1. Step 1: Understand private property access inside class

    Private properties can be accessed inside class methods using $this->property.
  2. Step 2: Check method reveal() returns private property

    The reveal() method returns $this->secret, which is 'hidden'. So echo prints 'hidden'.
  3. Final Answer:

    hidden -> Option D
  4. Quick Check:

    Private accessed inside class method = allowed [OK]
Hint: Private properties accessible inside class methods [OK]
Common Mistakes:
  • Thinking private properties can't be accessed even inside class
  • Expecting error when accessing private inside class
  • Confusing property name with string output
4. Identify the error in this PHP code:
class ParentClass {
    protected $value = 10;
}

class ChildClass extends ParentClass {
    public function getValue() {
        return $this->value;
    }
}

$obj = new ParentClass();
echo $obj->value;
medium
A. Syntax error in class declaration
B. ChildClass cannot extend ParentClass
C. Cannot access protected property from outside the class
D. No error, outputs 10

Solution

  1. Step 1: Understand protected property access

    Protected properties can be accessed inside the class and child classes, but not from outside.
  2. Step 2: Check code usage

    The code tries to echo $obj->value where $obj is ParentClass instance, accessing protected property from outside, causing error.
  3. Final Answer:

    Cannot access protected property from outside the class -> Option C
  4. Quick Check:

    Protected outside class = error [OK]
Hint: Protected properties not accessible outside class or children [OK]
Common Mistakes:
  • Trying to access protected property directly from object
  • Confusing protected with public
  • Assuming no error when accessing protected externally
5. Given the following PHP classes, what will be the output?
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();
hard
A. base & protected info & base
B. base & protected info & Error accessing private property
C. Error accessing private property & protected info & base
D. Error accessing private property & Error accessing protected property & Error

Solution

  1. Step 1: Analyze private property access in Derived

    Private property $data is only accessible inside Base class. Derived cannot access $this->data directly.
  2. 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).
  3. Final Answer:

    base & protected info & Error accessing private property -> Option B
  4. Quick Check:

    Private inaccessible in child = error [OK]
Hint: Private properties not accessible in child classes [OK]
Common Mistakes:
  • Assuming private properties are inherited and accessible
  • Confusing protected with private access
  • Expecting no error when accessing private from child
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard