Challenge - 5 Problems
PHP Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code with private property?
Consider this PHP class with a private property. What will be the output when trying to access the private property directly from outside the class?
PHP
<?php class Box { private $content = 'secret'; } $box = new Box(); echo $box->content; ?>
Attempts:
2 left
💡 Hint
Private properties cannot be accessed directly outside the class.
✗ Incorrect
In PHP, private properties are only accessible inside the class itself. Trying to access them from outside causes a fatal error.
❓ Predict Output
intermediate2:00remaining
What will this PHP code output when accessing a protected property from a subclass?
Look at this PHP code where a subclass tries to access a protected property from its parent. What is the output?
PHP
<?php class Animal { protected $type = 'mammal'; } class Dog extends Animal { public function getType() { return $this->type; } } $dog = new Dog(); echo $dog->getType(); ?>
Attempts:
2 left
💡 Hint
Protected properties are accessible in subclasses.
✗ Incorrect
Protected properties can be accessed inside the class and its subclasses, so the method returns 'mammal'.
🔧 Debug
advanced2:00remaining
Why does this PHP code cause an error when accessing a static private property?
This PHP code tries to access a private static property from a subclass. What error occurs and why?
PHP
<?php class ParentClass { private static $count = 0; public static function increment() { self::$count++; } public static function getCount() { return self::$count; } } class ChildClass extends ParentClass { public static function increment() { self::$count++; } } ChildClass::increment(); echo ChildClass::getCount(); ?>
Attempts:
2 left
💡 Hint
Private static properties are not accessible in subclasses.
✗ Incorrect
Private static properties belong only to the class where they are declared. The subclass cannot access ParentClass::$count, causing a fatal error.
📝 Syntax
advanced2:00remaining
Which option correctly declares a public property with a default value in PHP 8.1+?
Choose the correct syntax to declare a public property with a default value in a PHP class using typed properties.
Attempts:
2 left
💡 Hint
Typed properties use type before the variable name with an equals sign for default.
✗ Incorrect
In PHP 7.4+, typed properties are declared as 'public string $name = "John";'. Other options have invalid syntax.
🚀 Application
expert2:00remaining
How many properties are accessible from outside the class in this PHP code?
Given this PHP class with different visibility properties, how many properties can be accessed directly from an instance outside the class?
PHP
<?php class Car { public $make = 'Toyota'; protected $model = 'Corolla'; private $year = 2020; } $car = new Car(); // How many properties can be accessed directly like $car->property ? ?>
Attempts:
2 left
💡 Hint
Only public properties are accessible from outside the class.
✗ Incorrect
Only the public property $make can be accessed directly. Protected and private properties are not accessible outside the class.