0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.