Thoughts about __toString() in PHP

PHP is a loosely typed language(the same variable can be a string or number), but sometimes I think it should be even more loose in some aspects and allow even more magic. Maybe that was a wrong way of putting it, but especially in the case of __toString() in PHP I think that the developers copied Java without thinking the possibilities of the method, what it could have been if it was not bound to “String”. “String” is this old thing, actually one of the first things, that was created out of 0 and 1.

If you’re not familiar with __toString() method, I’ll first explain what it does. The __ tells you that this is a magic method, and it is a feature provided in an object in PHP and many other languages. The point of __toString() is to return a string value of the object if it is in a string context, or simply put, echoed. Example:


class Person {
private $fname = 'John';
private $lname = 'Doe';
public function __toString(){
return $this->fname.' '.$this->lname;
}
}

$person = new Person();
echo $person; //John Doe

For me this gives my object a new dimension. My Object is a string and an object. And I’m mostly fine with it, more than that, I love the feature.

What I would like to change is to change the __toString() method into __toValue(), or add more functions like __toArray() and __toObject() or __toWhatever() that can be used if the __toString is not set or/and in a prioritized order(I say if the __toString() is not set, because it should not be deprecated at first).

Let’s make __toArray() the example. I would like my object to be interpreted as an array in some cases if the object way is not supported. The best way to show what I mean is to use an example:


class Person {
private $fname = 'John';
private $lname = 'Doe';
public function __toArray(){
return array('fname'=>$this->fname, 'lname'=>$this->lname);
}
}
$person = new Person();
//NOT VALID PHP
echo $person['fname']; //John

//VALID PHP
$person = $person->__toArray();
echo $person['fname']; //John

As you can see, the idea is likeable – and I’m not the first one to propose the add-on. But why do I like this?
– Because it gives an Object more functionality
– You can do much more cool stuff without having to create and call functions for them
– Gives objects multiple dimensions, and makes them more rich
– Cool if you need to transform an Object into different types of structures and easily append into different situations
– You could use an Object in new places, like directly in a foreach loop
– Why should this only work with strings?
– Can give access to private and protected values of the object where casting((array)Object) does not work
– __toString() is actually faster than calling a public method toString() that does the same.
– Great way of simplifying some frameworks
– __toString() limits the use of something that should not be limited in my opinion

Why not?
– It’s not that hard to make an own toArray() method that does the magic without being magic
– Magic methods may add some more “WTF?” to some programmers
– (whispering carefully from a corner)We should not use magic methods that much? (Why not?)

Well, I really can’t think of many bad things with some extra magic. Are there other arguments for or against?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: