Page 2 of 2
Re: Protoype OOP in PHP
Posted: Mon Dec 14, 2009 5:01 am
by VladSun
josh wrote:VladSun wrote:
I've had such cases - I needed a toHash() method for every single object in my JS application. So, I extended the base object prototype.
Does it mean I've created a "god" object - don't think so.
Every object in the entire application

means your application is not layered and you have no differentiation between value/ entity objects.
__wakeup, __sleep, __toString,
__toHash, ....
Common, josh ... is your system layered enough regarding these methods ...
It's a system level method, not an application level one - you know it.
Re: Protoype OOP in PHP
Posted: Tue Dec 15, 2009 1:21 am
by josh
I guess I don't understand why you would need to serialize or hash every single object in a system, including temporary data transfer and method objects, and objects that don't have a logical identity and only serve for utility or to assist decomposition. If objects share a concept like "identity" in common they are entity classes and you should have an entity layer super class (POEAA)
Or to look at it from another angle, you would have to first ask yourself WHY am I hashing all these objects? Since I don't know what your answer is exactly I will assume it is for caching since you mention serializing. In that case I would say the hash method could possibly be moved to the cache class, if a layer super type would be "cross cutting".
with a prototypical solution for code re-use, other developers might start using your hash method for something it was never intended for, like comparing equality. Then later when you change your implementation for caching you might break a bunch of equality methods for instance, so that would be one benefit to moving the method to the class that uses it.
I would say interfaces solve the problem better in an interesting way too, because you won't necessarily be able to use the same _toHash method for every class, maybe the different properties that are used to generate the hash could depend on which class you are hashing. An interface would just define the contract.
I would strongly prefer moving the method to the cache class in this example though. It is highly coupled to that class so that is my preference. Once that is done it is suddenly no longer cross cutting and you realize thats where it really belongs, not "all over the system"
Re: Protoype OOP in PHP
Posted: Tue Dec 15, 2009 2:40 am
by Christopher
josh wrote:I guess I don't understand why you would need to serialize or hash every single object in a system
Such a statement could be made for __toString() or even __construct() which are not needed for every single object in a system. I think Vlad's point what: Where do you draw the line? And there is guaranteed to be disagreement on that.
Certainly __toArray() would be very handy in PHP given that arrays are a major data type and are used in core language constructs like foreach().
Re: Protoype OOP in PHP
Posted: Tue Dec 15, 2009 4:29 am
by josh
Couldn't that already be done today with Zend/PHP extensions?
And by the way I specifically said adding multiple naturally cohesive methods ( I worded it as a "collection of methods") is bad to do with prototyping.
For instance if you had methods that were coupled
->voteUp()
->voteDown()
->hasVotedUp()
->hasVotedDown()
->getVotes()
->getVoteScore()
etc....
prototyping makes it seem like we should just add these to every dang object that needs them, when the *better* thing to do IMO is come up with a name for this concept... how about .. "ballot", too hard to spell, how about "tally", and we could make a Votable interface that defines setTally() and getTally(), the Tally object then holds important business logic about voting, without polluting each votable object's method list.
something like the __toHash is something that should be addressed at the language level. I guess some languages like scheme let you modify the language *at runtime*, but I would rather the PHP developers focus on more important stuff when there is a perfectly fine C++ extension architecture in place already.
Prototyping is just easily abused, and not needed, thats all... It can sometimes lead to god objects when inexperienced developers touch it, but like I said its useful in the event you can't modify what you want to change (the Window class is under each browser's control so if we need extra methods we'd have to wrap it), but that falls within "Developers not programming right" again (although it we weren't the ones doing it wrong in that case).
Also your example of __construct or __sleep confuse me because those are interfaces. Prototyping involves using the same function on multiple "classes", am i confused? I would consider __sleep an event call back, its just an interface, not an implementation
Re: Protoype OOP in PHP
Posted: Tue Dec 15, 2009 1:32 pm
by Christopher
josh wrote:something like the __toHash is something that should be addressed at the language level.
Again, you keep drawing the line where you think it should be. Prototyping or Mixins allow the programmer to decide where the line is to be drawn.
Re: Protoype OOP in PHP
Posted: Tue Dec 15, 2009 11:10 pm
by josh
I guess I fail to see how prototyping enables you to make hook/callback methods, or is even related in any way. Im not drawing any line anywhere, to the contrary I said prototyping is extremely useful... for working with code that is not extensible that you cannot change. Thats the only valid use case I can think of. I'm just saying there's nothing that can be done in a prototypical language that can't be done in regular OOP (with a different implementation albeit) without the same amount amount of code
And we haven't even started talking about the other downsides to prototypical inheritance, it completely breaks regular class based inheritance (at least the javascript implementation of it, which is the only one I am familiar with). The fact you need a framework to use inheritance is a red flag to me.
Re: Protoype OOP in PHP
Posted: Sun Dec 20, 2009 8:47 am
by josh
Jenk wrote:
That combined with create_function() etc could well give you a prototype object, only without the short-hand syntax.
I was critiquing
an entry in the critique forums today and ran across something I had never seen in PHP before. Take a look at his posting. PHP DOES have the short hand syntax now!
Check out this article
http://www.ibm.com/developerworks/opens ... index.html
Combined with reflection you're 99% there. Sort of. The __invoke() callback is pure gravy too. I think this would make everyone happy!
a new getClosure() method has been added to the ReflectionMethod and ReflectionFunction classes in PHP for dynamically creating closure from the specified function or method.
I like it because it makes your "prototyped" methods execute within the scope of which they are defined, which completely averts all the negative arguments I have about prototyping.
Re: Protoype OOP in PHP
Posted: Sun Dec 20, 2009 10:04 am
by VladSun
josh wrote:I like it because it makes your "prototyped" methods execute within the scope of which they are defined, which completely averts all the negative arguments I have about prototyping.

Nice one, josh!
That's what I was missing too
