0
0
PHPprogramming~10 mins

__toString for string representation in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AtoString
B__toStr
C__toString
D__string
Attempts:
3 left
💡 Hint
Common Mistakes
Using toString without underscores
Misspelling the method name
Using __string instead of __toString
2fill in blank
medium

Complete 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'
A"Person: $name, $age"
B"Name: $this->name, Age: $this->age"
C"$this->name is $this->age years old"
D"Name: name, Age: age"
Attempts:
3 left
💡 Hint
Common Mistakes
Using variables without $this->
Using undefined variables
Not returning a string
3fill in blank
hard

Fix 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'
A.
B,
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + for string concatenation
Using comma instead of dot
Using dash instead of dot
4fill in blank
hard

Fill 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'
A.
B+
C" "
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of . for concatenation
Not adding a space between names
Using comma instead of space string
5fill in blank
hard

Fill 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'
A"Name: " . $this->firstName . " " . $this->lastName
B"Email: " . $this->email
C" - "
D"Name: "
Attempts:
3 left
💡 Hint
Common Mistakes
Not concatenating strings properly
Missing spaces or separators
Using + instead of . for concatenation