The __toString method lets you decide how an object turns into a string. This helps when you want to print or show an object in a simple way.
__toString for string representation in PHP
public function __toString(): string { // return a string representation of the object }
The method must be public and return a string.
It is called automatically when the object is used as a string.
Person class that returns the person's name when printed.<?php class Person { private string $name; public function __construct(string $name) { $this->name = $name; } public function __toString(): string { return "Person: " . $this->name; } } $p = new Person("Anna"); echo $p;
Product class that returns a string with the product title and price.<?php class Product { private string $title; private float $price; public function __construct(string $title, float $price) { $this->title = $title; $this->price = $price; } public function __toString(): string { return "$this->title costs {$this->price}"; } } $product = new Product("Book", 12.99); echo $product;
This program creates a Car object and prints it. The __toString method defines how the car looks as a string.
<?php class Car { private string $make; private string $model; public function __construct(string $make, string $model) { $this->make = $make; $this->model = $model; } public function __toString(): string { return "Car: $this->make $this->model"; } } $car = new Car("Toyota", "Corolla"); echo $car;
If you try to print an object without __toString, PHP will give an error.
Use __toString to make objects easier to read and debug.
__toString lets you control how an object turns into a string.
It is called automatically when printing or converting an object to string.
Always return a string from __toString.
Practice
What is the main purpose of the __toString method in a PHP class?
Solution
Step 1: Understand the role of
The__toString__toStringmethod is a special function in PHP that defines how an object should be represented as a string.Step 2: Identify its automatic use
This method is called automatically when you try to print or convert an object to a string.Final Answer:
To define how an object is converted to a string automatically -> Option BQuick Check:
__toStringcontrols string conversion [OK]
- Confusing __toString with constructor
- Thinking it handles errors
- Assuming it creates new objects
Which of the following is the correct syntax to declare the __toString method inside a PHP class?
class Example {
?
}Solution
Step 1: Check method name and visibility
The method must be named exactly__toStringand usually declared public.Step 2: Verify return type and return value
It must return a string, so the return type: stringis correct, and the return value must be a string.Final Answer:
public function __toString() : string { return "Example"; } -> Option AQuick Check:
Correct syntax includes public, __toString, returns string [OK]
- Omitting public visibility
- Using wrong method name like toString
- Returning non-string values
- Using echo instead of return
What will be the output of the following PHP code?
class Person {
private string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function __toString() : string {
return "Person: " . $this->name;
}
}
$p = new Person("Alice");
echo $p;Solution
Step 1: Understand the __toString method
The__toStringmethod returns the string "Person: " concatenated with the name property.Step 2: Check the echo statement
When echoing the object, PHP calls__toStringautomatically, so it prints the returned string.Final Answer:
Person: Alice -> Option CQuick Check:
echo object calls __toString, prints combined string [OK]
- Expecting default object print
- Forgetting __toString is called automatically
- Confusing property access with direct print
Identify the error in the following PHP class that implements __toString:
class Product {
private string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function __toString() {
echo $this->name;
}
}
$p = new Product("Book");
echo $p;Solution
Step 1: Check __toString method implementation
The__toStringmethod must return a string, but here it usesechowhich outputs directly and returns nothing.Step 2: Understand consequences of echo inside __toString
Using echo causes a fatal error because PHP expects a string return from__toString.Final Answer:
The __toString method should return a string, not echo it -> Option AQuick Check:
__toString must return string, not echo [OK]
- Using echo instead of return in __toString
- Ignoring return type requirements
- Assuming echo works inside __toString
You want to create a PHP class Book that stores a title and author. When printing the object, it should show "Title by Author". Which implementation of __toString correctly achieves this?
class Book {
private string $title;
private string $author;
public function __construct(string $title, string $author) {
$this->title = $title;
$this->author = $author;
}
public function __toString() : string {
?
}
}Solution
Step 1: Understand string concatenation in PHP
PHP uses the dot operator.to join strings, not plus+.Step 2: Check return vs echo
The__toStringmethod must return a string, soreturnis correct, notecho.Step 3: Choose correct concatenation and return
return $this->title . ' by ' . $this->author; correctly returns the concatenated string using dots.Final Answer:
return $this->title . ' by ' . $this->author; -> Option DQuick Check:
Use return with dot concatenation in __toString [OK]
- Using echo instead of return
- Using + for string concatenation
- Forgetting to return a string
Ready to prove it?
Pass the test and this pattern moves unknown
