Challenge - 5 Problems
PHP __toString Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of __toString method in PHP class
What is the output of this PHP code when executed?
PHP
<?php class Fruit { private string $name; public function __construct(string $name) { $this->name = $name; } public function __toString(): string { return "Fruit: " . $this->name; } } $apple = new Fruit("Apple"); echo $apple; ?>
Attempts:
2 left
💡 Hint
The __toString method defines how the object converts to a string.
✗ Incorrect
The __toString method returns 'Fruit: ' concatenated with the private property name. When echoing the object, PHP calls __toString automatically.
❓ Predict Output
intermediate2:00remaining
Behavior of __toString with concatenation
What will be the output of this PHP code?
PHP
<?php class Person { private string $firstName; private string $lastName; public function __construct(string $first, string $last) { $this->firstName = $first; $this->lastName = $last; } public function __toString(): string { return $this->firstName . ' ' . $this->lastName; } } $p = new Person("John", "Doe"); echo "Hello, " . $p . "!"; ?>
Attempts:
2 left
💡 Hint
Concatenation with a string triggers __toString method.
✗ Incorrect
When concatenating an object with a string, PHP calls __toString to get the string representation. Here it returns first and last name separated by space.
🔧 Debug
advanced2:00remaining
Identify the error in __toString implementation
What error will this PHP code produce when executed?
PHP
<?php class Book { private string $title; public function __construct(string $title) { $this->title = $title; } public function __toString() { return $this->title; } } $book = new Book("1984"); echo $book; ?>
Attempts:
2 left
💡 Hint
Check the return type declaration in __toString method.
✗ Incorrect
The __toString method returns a string and does not declare a return type. This is allowed in PHP. So no error occurs and output is the title.
📝 Syntax
advanced2:00remaining
Which __toString method causes a syntax error?
Which option below contains a syntax error in the __toString method?
Attempts:
2 left
💡 Hint
The __toString method must return a string, not echo it.
✗ Incorrect
Option A uses echo inside __toString but does not return a string. This causes a fatal error because __toString must return a string.
🚀 Application
expert3:00remaining
Count characters using __toString in a class
Given this PHP class, what is the value of $length after execution?
PHP
<?php class Message { private string $text; public function __construct(string $text) { $this->text = $text; } public function __toString(): string { return strtoupper($this->text); } } $msg = new Message("hello world"); $length = strlen((string)$msg); ?>
Attempts:
2 left
💡 Hint
strlen counts characters in the string returned by __toString.
✗ Incorrect
The __toString method returns the uppercase version of 'hello world' which is 'HELLO WORLD' with length 11 including the space.