Why would a programmer need to use the function? How would the same results have been achieved in php 4?
Asked
Active
Viewed 111 times
2 Answers
1
In any scenario where you want to control how an object behaves when used in a string context (used as a string), e.g.
class FullName
{
protected $firstName;
protected $middleNames = array();
protected $lastName;
// ... methods ...
public function __toString()
{
return sprintf('%s %s %s', $this->firstName,
implode(' ', $this->middleNames),
$this->lastName);
}
}
$fullname = new FullName('John', array('Jim', 'Jamie'), 'Jackson');
echo "Hello, my name is $fullname";
You cannot simulate this method in PHP4. In fact, you shouldn't even be using PHP4 anymore at all.

Gordon
- 312,688
- 75
- 539
- 559
-
Wow neat, I learned something today! (p.s. sometimes we still don't have a choice but to use php4) – Rob Mar 25 '10 at 18:37