Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stringable

stringable is a type, which represents data that may be used as a string: either a string, a heredoc or a string constant; or an object which has the __toString() method.

Stringable is automatically added to any class and subclasses that has the __toString() method, though it is recommended to add it explicitly.

<?php

    function trumpet(Stringable $s) {
        print $s;
    }

    class Elephpant implements Stringable {
        function __toString() {
            return __METHOD__;
        }
    }

    trumpet('foo');    // foo
    trumpet(new Elephpant());  // Elephpant::__toString

?>

Documentation

See Also