|
Posted by Jerry Stuckle on 06/12/07 21:03
gosha bine wrote:
> 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.
>
>
Now you're playing with semantics. I'm not confusing them. The fact
remains - PHP knows how to convert built-in types. It does not know how
to convert user-defined objects.
And you're right - 'echo some_object' doesn't make sense - that's why
they took away the default conversion - it didn't make sense.
>>>>
>>>> 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.
>
Try putting some variables in your class and see what happens.
>
>> 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?
>
So, if it's useless, why not take it away? Why bother to maintain
something which has no use?
>>
>>> 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.
>
I suspect because it's useless. Shouldn't have been there in the first
place, and now they're correcting that.
>>
>>>> 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.
>
Tell me ONE LANGUAGE - including PHP - where you don't have to think to
use it.
>>>>
>>>> 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?
>
They did. They gave you the _tostring() function to do whatever you
want with it.
>
>> 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.
>
Hasn't broken any of my code. But if you code sloppily, then yes, it
will break your code.
> > 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.
>>
>
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|