|
Posted by gosha bine on 06/12/07 17:44
On 12.06.2007 17:06 Jerry Stuckle wrote:
>> I meant, every other php type supports implicit toString:
>>
>> echo 123; - works
>> echo array(1, 2, 3); - works
>> echo fopen('blah', 'r'); - works
>>
>> echo new Blah(); - DOES NOT work
>>
>
> Sure, and every other type is built in.
>
Don't confuse 'type' and 'class'. 'Blah' is a class, 'object' is a
built-in type. 'echo some_resource' doesn't make more sense than 'echo
some_object', however it remains legal.
>>>
>>> And as for every other comparable programming language - no they don't.
>>
>> Most languages support implicit toString for objects:
>>
>> alert(new Blah()) - works in javascript
>> puts Blah.new - works in ruby
>> print Blah() - works in python
>> System.out.println(new Blah()) - works in java
>>
>> echo new Blah() - DOES NOT work in php
>>
>
> Well, for instance, C++ doesn't support it.
#include <iostream>
class A {};
int main() {
A* a = new A();
std::cout << a;
}
Works for me.
> And I've found Java's
> default tostring() function to be particularly useless. That's why I
> overload it in every class where I'm going to need it.
Again, there's a difference between 'useless' and 'forbidden'. There are
many examples of completely useless code, do you think it all should be
forbidden at the language level?
>
>> Are you saying other languages' designers care less about their
>> programmers? Why do they allow such a "useless" thing?
>>
>
> I'm not saying anything about any other languages' designers. All I'm
> saying is I find it particularly useless.
And I wasn't talking about implicit toString being useful or not. I'm
just looking for the reason why it was taken out of the language.
>
>>> But who cares? Other languages have features PHP doesn't have, and
>>> PHP has features they don't have. It's comparing apples and oranges.
>>
>> Comparing programming languages and technologies is always productive,
>> there's always something to learn from each other.
>>
>
> Sure. But different languages have different syntax and different
> features. That's why they're different languages. No one language is
> good for everything.
An interesting thought. Can I quote you on that? ;)
>
>>>
>>> Personally, I've found the silent conversion allows for sloppy
>>> programming and problems (like the one which started this thread).
>>> I'm glad to see the implicit conversion is gone.
>>
>> What exactly are the problems which are solved by removing the silent
>> conversion?
>>
>
> It makes you think about the implementation of the _tostring() method -
> and specifically stops programming errors like which started this thread.
>
Thank you, I don't need a language feature that "makes me think". I
prefer to spend my time thinking about my problem domain, about my
algorithms and code structure and I'm not interested in "building
scaffolding to support the language itself" (c) Dave Thomas.
>>>
>>> But if you really want it, the developers left a way for you to do
>>> so. Good for them.
>>>
>>
>> An object without toString was converted to "object #x" or similar
>> when being printed. So was the behaviour prior to 5.2. Useless or not,
>> it simply worked. Now, it's broken and there's no way to make it work
>> again.
>>
>>
>
> Yea, it was broken before. "object #x" is real descriptive, isn't it?
And why didn't they make it more descriptive and useful?
> And of course, you still have var_dump() and print_r() to help you,
> don't you?
>
> I just see absolutely no problem with removing the default.
I wouldn't call it a problem. It's just a small design bug, that breaks
some (not much) old code and makes php a bit more unpredictable and
inconsistent than before.
> I would
> have more of a problem if they didn't allow the programmer a means to
> implement it, however.
>
> But heck - I seldom used it. Last time I can think of using it was for
> other than debugging was in a simple Point class, where it would print
> "(x,y)" or "(r, theta)", depending on whether I was using Cartesian or
> Polar coordinates.
>
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Navigation:
[Reply to this message]
|