Challenge - 5 Problems
PHP Class Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple PHP class method call
What is the output of this PHP code?
PHP
<?php class Greeting { public function sayHello() { return "Hello, world!"; } } $greet = new Greeting(); echo $greet->sayHello(); ?>
Attempts:
2 left
💡 Hint
Look at the method name and how it is called on the object.
✗ Incorrect
The class Greeting has a public method sayHello() that returns the string "Hello, world!". The object $greet calls this method correctly, so the output is exactly that string.
❓ Predict Output
intermediate2:00remaining
Output when accessing a private property directly
What happens when this PHP code runs?
PHP
<?php class Person { private string $name = "Alice"; } $p = new Person(); echo $p->name; ?>
Attempts:
2 left
💡 Hint
Check the visibility of the property and how it is accessed.
✗ Incorrect
The property $name is declared private, so it cannot be accessed directly outside the class. Trying to echo $p->name causes a fatal error about accessing a private property.
🔧 Debug
advanced2:00remaining
Identify the syntax error in this class declaration
This PHP code has a syntax error. Which option shows the exact error message produced when running it?
PHP
<?php class Car { public $make; public $model public function __construct($make, $model) { $this->make = $make; $this->model = $model; } } ?>
Attempts:
2 left
💡 Hint
Look carefully at the property declarations and missing punctuation.
✗ Incorrect
The property $model is missing a semicolon at the end of its declaration line, so the parser finds 'public' keyword unexpectedly on the next line, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output of static property access in PHP class
What is the output of this PHP code?
PHP
<?php class Counter { public static int $count = 0; public function __construct() { self::$count++; } } new Counter(); new Counter(); echo Counter::$count; ?>
Attempts:
2 left
💡 Hint
Static properties are shared across all instances of the class.
✗ Incorrect
Each time a new Counter object is created, the static property $count is incremented by 1. After creating two objects, $count is 2.
❓ Predict Output
expert2:00remaining
Output of class constant and method call
What is the output of this PHP code?
PHP
<?php class MathConstants { public const PI = 3.14; public static function getPi() { return self::PI; } } echo MathConstants::PI . ' ' . MathConstants::getPi(); ?>
Attempts:
2 left
💡 Hint
Class constants are accessed with :: and can be used inside static methods with self::
✗ Incorrect
The constant PI is defined as 3.14. Accessing MathConstants::PI returns 3.14. The static method getPi() returns self::PI, also 3.14. So the output is '3.14 3.14'.