First off, I won't comment on the bullshit that PHP guy said. The idea of a "toXML()" method in a base class is actually quite revealing of the "usual PHP way to do things" : how can a software architect even think about such a crappy stuff ???
The guy probably learned "computer science" through HTML and scripting languages, and obviously he understands nothing about OO concepts.
The discussion about aspects is far more interesting -:)
Take for example the thread notification and "toString()" methods in Java's Object. Yes, sometimes these methods are very useful. The rest of the time they are baggage.
They're always useful, I've never seen a program that does not use either toString() or threads, either directly or indirectly !
* Being able to "stringify" itself is a pretty good feature that every oject should have IMHO. toString() allows this. Try doing the equivalent in C++ and you'll end by redefining your own base class !!!
* Java Threads are unified, and part of the runtime, unlike in C++ once again. Try threads in C++ and you'll thank god for the threads in Java, and the methods you have in Object. I don't see any problem in having thread-related methods in Object when you have chosen a language that deals easily with threads !
Even when you need them, it would be nice if you could substitute (or pick) different implementations (like an XML string format...).
Well override toString() then, but I would use interfaces instead in this specific case :
public interface XMLizable {
public String toXML();
}
But most important is the fact that these methods have no relationship to the application logic (except in special cases).Features like these are "cross-cutting concerns", in aspect-oriented programming (AOP) terminology.
Well that's quite a discussion : how can you use aspects to serialize an instance to XML without breaking encapsulation in 99% of the situations (private/protected stuff) ?
Also, converting an object to a specific data format (XML or else) *is* precisely application logic to me ! This is part of the object's behavior to be able to do such thing !!! It should be clear in the object's *contract*, that's why the use of interfaces is the best way to do this IMHO.
So, I would leave almost everything out of the "ultimate base class" but make it composable with aspects.
Aspects are good for certain domains (e.g. transactions or transversal security), but they don't send all OO concepts to trash.
What about equals & hashCode ? These ones are freakin' great when you have to deal with collections (once again, make a parallel with C++ and STL's collections if you have experienced it, it's a self explanatory exercise :-) ) and I don't think AOP can do anything for you here :-/
Well, would you say "I don't need to compare for object equality in every object so forget about equals()" ???
Or "I don't always need collections so don't bother with equals() / hashCode()" ???
I personnally think the guys @Sun made it quite right with java.lang.Object, and I'm glad there were no PHP "architects" in the consortium then :-P
Have fun,
Remi