# [patch] __string() method

- Thread: https://externals.io/message/4346
- Original archive: https://news-web.php.net/php.internals/4346
- Started by Jon Parise on 2003-09-02T08:29:11+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #4346 — Jon Parise — 2003-09-02T08:29:11+00:00

URL: https://externals.io/message/4346#4346

Attached is a fairly simple (and rough) patch that adds support for a
"special" __string() object method to ZE2.  I wrote it for fun, but I
thought there might be general interest in the idea, so I'm posting it
here.  The idea is inspired by Python's __str__ method [1].

Here's how it works in practice:

    class Foo { }

    class Stringy extends Foo
    {
        function __string()
        {
            return 'blah';
        }
    }

    $foo = new Foo();
    $stringy = new Stringy();

    print "$foo\n";
    print "$stringy\n";


    $a = (string)$foo;
    $b = (string)$stringy;

    print "$a\n";
    print "$b\n";

Output:

    Object id #1
    blah
    Object id #136238812
    blah

So, essentially, if an object offers a __string() method, it will be
used whenever a string representation of the object is requested.  I
can see this being useful for things like object serialization or as a
shortcut for things like the Exception class's toString() method.

The patch is attached.  Comments are welcome.

[1] http://www.python.org/doc/current/ref/customization.html

-- 
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)

---

## #4351 — Jon Parise — 2003-09-02T15:45:29+00:00

URL: https://externals.io/message/4346#4351

> JP> Attached is a fairly simple (and rough) patch that adds support for a
> JP> "special" __string() object method to ZE2.  I wrote it for fun, but I
> JP> thought there might be general interest in the idea, so I'm posting it
> JP> here.  The idea is inspired by Python's __str__ method [1].
> 
> First why not name it toString() since for now we have consensus on sudlyCaps.
> But i didn't bother with names yet when i made this patch:
> http://marcus-boerger.de/php/ext/ze2/ze2-iterators-20030902.diff.txt

I chose '__string' to follow the existing convention for "special"
method names (e.g. __construct, __clone, __call).  Also, I suspect
that many developers are already using toString() methods in their
classes, and I'm not sure we want to use them directly with having
them think about the semantics of their decision first.
 
> Second i think you should use the cast facility like i did in my patch.
 
I looked at the cast facility briefly and decided not to use it
because it meant including a new zend_std_* implementation for casting
(that function entry is currently `NULL` in zend_object_handers.c).

Admittedly, I wasn't comfortable at the time with writing a universal
casting handler, and because I only wanted to deal with string
conversions, I decided to just implement it as a separate dedicated
function entry.

-- 
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)

---

## #4352 — Jon Parise — 2003-09-02T15:47:44+00:00

URL: https://externals.io/message/4346#4352 · In reply to: #4351

> I chose '__string' to follow the existing convention for "special"
> method names (e.g. __construct, __clone, __call).  Also, I suspect
> that many developers are already using toString() methods in their
> classes, and I'm not sure we want to use them directly with having
                                                         ^without

> them think about the semantics of their decision first.
  
-- 
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)
