__toString and casting

I am a bit confused about the purpose of the __toString() magic method in PHP. As I understand it, the methd should be called each time an object is converted to a string.

However, in practice, the invokation of this method seems very sporadic. Here is what I have found. I am using PHP 5.1.2 on Windows 2000 running through an Apache 2 server.

class Test
{
public function __toString()
{
return 'string cast';
}
}

$o = new Test;

echo($o); // __toString() invoked
echo($o . ' '); // __toString() not invoked
$s = ((string) $o);
echo($s); // __toString() not invoked


I would have expected the __toString method to have been called in all the above cases. When I use SimpleXML however, which I can only assume, uses the __toString method when objects are casted, the above behviour is as expect. :confused:

Is the simple xml using some secret method? And if so, what is it?Hi, have you looked at this page: <!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.oop5.magic.php">http://www.php.net/manual/en/language.oop5.magic.php</a><!-- m -->

Example 19-28 explains when __toString() is and is not called.

It is worth noting that the __toString method will only be called when it is directly combined with echo() or print().

Example 19-28. Cases where __toString is called
<?php
// __toString called
echo $class;

// __toString called (still a normal parameter for echo)
echo 'text', $class;

// __toString not called (concatenation operator used first)
echo 'text' . $class;

// __toString not called (cast to string first)
echo (string) $class;

// __toString not called (cast to string first)
echo "text $class";
?>Sorry, I have seen that page. I am curious to know how it is done in the simple xml extension, it would also be logical to have toString convert the object to a string when it is cast to a string too.I am guessing that SimpleXML does it in the extension, at the C level.

SimpleXML does some other things that PHP does not allow on its own either, like a variable being both an object and an array.

For example you can have $SimpleXML->nodeName and $SimpleXML['attribute'] at the same time. I have tried experimenting to reproduce this behavior but all my tests in pure PHP have resulted in very quirky results. So it is most likely done directly in the extension, which I would say the same is true for the __toString for it.I use the ArrayAccess interface often which allows you to access an object as if it were an arry like in simplexml. It is part of the SPL extension. You declare your objects to implement the ArrayAccess interface and define all its methods.

class Test implements ArrayAccess
{
public function offsetExists($offset)
{

}

public function offsetGet($offset)
{

}

public function offsetSet($offset, $value)
{

}

public function offsetUnset($offset)
{

}
}

Your explanation of it being coded at the extension level does make sense now. That never occured to me :(. Thanksexcept simpleXML does not implement ArrayAccess according to the documentation, which is why I concluded that it is doing it on a lower level in the extension.
 
Top