0
0
PHPprogramming~20 mins

__toString for string representation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP __toString Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
?>
AObject of class Fruit could not be converted to string
BFruit: Apple
CApple
DFatal error: Cannot access private property Fruit::$name
Attempts:
2 left
💡 Hint
The __toString method defines how the object converts to a string.
Predict Output
intermediate
2: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 . "!";
?>
AHello, John!
BHello, Person Object!
CFatal error: Object of class Person could not be converted to string
DHello, John Doe!
Attempts:
2 left
💡 Hint
Concatenation with a string triggers __toString method.
🔧 Debug
advanced
2: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;
?>
ANo error, outputs: 1984
BFatal error: Method Book::__toString() must return a string value
CWarning: __toString method missing return statement
DParse error: syntax error in __toString method
Attempts:
2 left
💡 Hint
Check the return type declaration in __toString method.
📝 Syntax
advanced
2:00remaining
Which __toString method causes a syntax error?
Which option below contains a syntax error in the __toString method?
Apublic function __toString(): string { echo "Hello"; }
Bpublic function __toString() { return "Hello"; }
Cpublic function __toString(): string { return "Hello"; }
D} ;"olleH" nruter { gnirts :)(gnirtSot__ noitcnuf cilbup
Attempts:
2 left
💡 Hint
The __toString method must return a string, not echo it.
🚀 Application
expert
3: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);
?>
AFatal error: Object cannot be converted to string
B10
C11
D0
Attempts:
2 left
💡 Hint
strlen counts characters in the string returned by __toString.