Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the __toString method that returns the name property.
PHP
<?php class Person { private $name; public function __construct($name) { $this->name = $name; } public function [1]() { return $this->name; } } $person = new Person("Alice"); echo $person; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using toString without underscores
Misspelling the method name
Using __string instead of __toString
✗ Incorrect
The magic method __toString() is used in PHP to define how an object is converted to a string.
2fill in blank
mediumComplete the code to return a formatted string with the person's name and age in the __toString method.
PHP
<?php class Person { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function __toString() { return [1]; } } $person = new Person("Bob", 30); echo $person; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variables without $this->
Using undefined variables
Not returning a string
✗ Incorrect
Using double quotes with $this->name and $this->age inside returns the formatted string with the object's properties.
3fill in blank
hardFix the error in the __toString method to correctly return the full name.
PHP
<?php class User { private $firstName; private $lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function __toString() { return $this->firstName[1]$this->lastName; } } $user = new User("John", "Doe"); echo $user; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + for string concatenation
Using comma instead of dot
Using dash instead of dot
✗ Incorrect
In PHP, the dot (.) operator is used to join strings together.
4fill in blank
hardFill both blanks to create a __toString method that returns the user's full name with a space between first and last names.
PHP
<?php class Member { private $firstName; private $lastName; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function __toString() { return $this->firstName[1][2]$this->lastName; } } $member = new Member("Jane", "Smith"); echo $member; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of . for concatenation
Not adding a space between names
Using comma instead of space string
✗ Incorrect
Use the dot (.) operator to join strings and include a space " " between the first and last names.
5fill in blank
hardFill all three blanks to create a __toString method that returns a formatted string with the user's full name and email.
PHP
<?php class Contact { private $firstName; private $lastName; private $email; public function __construct($firstName, $lastName, $email) { $this->firstName = $firstName; $this->lastName = $lastName; $this->email = $email; } public function __toString() { return [1] . [2] . [3]; } } $contact = new Contact("Emily", "Jones", "emily@example.com"); echo $contact; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not concatenating strings properly
Missing spaces or separators
Using + instead of . for concatenation
✗ Incorrect
Concatenate the label "Name: ", the full name with space, and then " - " followed by the email label and email.