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 foo(Stringable $s) {
        print $s;
    }
    
    class MyString implements Stringable {
        function __toString() {
            return __METHOD__;
        }
    }
    
    foo('foo');    // foo
    foo(new X());  // MyString::__toString

?>

Documentation

See Also