?
What will be the output of the following PHP code
What will be the output of the following PHP code
class Person
{
function getName() { return "Bob"; }
function getAge() { return 44; }
function __toString() {
$desc = $this->getName();
$desc .= " (age ".$this->getAge().")";
return $desc;
}
}
$person = new Person();
print $person;
class Person
{
function getName() { return "Bob"; }
function getAge() { return 44; }
function __toString() {
$desc = $this->getName();
$desc .= " (age ".$this->getAge().")";
return $desc;
}
}
$person = new Person();
print $person;
Answer & Solution
Correct Answer:
Option
C
By implementing a __toString() method, you can control how your objects represent themselves when printed. The method is invoked automatically when your object is passed to print or echo, and its return value is substituted.
Join the Discussion
Login to post a comment or share your explanation.
Login