5 ms·
PHP Object Oriented Programming Reinvented
- zalew 16y agook, someone has to point that out: POOP'R?
- pavel_lishin 16y agoHe did, in the second paragraph of his post. I kind of hope it catches on, just so I can tell people with a straight face that I'm familiar with "Pee Aytch Pooper".
- dhotson 16y agoI did mention that it was kind of unfortunate.. ;-)
- pavel_lishin 16y agoUnfortunate? Keep working on it, man. I want to say "PHPooper" during an interview some day.
- deleted 16y ago[deleted]
- alttab 16y agoIntersting idea, but what does this have that ruby or javascript doesn't?
- dangrossman 16y agoI don't think that's the point. He didn't create a new language, he is working at bringing that style of programming to PHP development.
- dhotson 16y agoYup, I could have implemented this in pretty much any language with closures and some sort of __call / method_missing magic. It's mostly just a fun hack to experiment with a different OO style. By no means would I recommend using it in any way. Although, that said. A little language on top of this would be kind of neat...
- zaphar 16y agoReading on my mobile so its hard to tell but it looks a little like prototype based OO. The dynamic languages all sort of lend themselves to build your own OO hacks like this. See Joose, Moose, and Cappucino for other examples.
- xyzzyz 16y agoThe Common Lisp Object System is based on similar principles. "The Art of the Metaobject Protocol" by Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow describes its architecture, along with protocol used to extend it. Despite it did not make it into ANSI spec, it is a de facto standard and is widely implemented.
- TamDenholm 16y agoThis may come off in a pejorative way but thats definitely not my intention... Is the intention here to make PHP more Ruby like? If so why?
- leftnode 16y agoI'm not the author, but it could be. As an 11 year PHP programmer, why not? Ruby certainly has some nice language features, is well respected, and has gained a lot of ground recently. Why not borrow some ideas?
- TamDenholm 16y agoYeah i agree, which was one of the reasons for asking. I'm a PHP guy myself, i've not taken the time to properly learn Ruby but i am aware of some of its features and i remember thinking at the time "thats very cool, i wish PHP did that", so yeah, totally with you.
- raganwald 16y agoUpmodded for asking an interesting question, namely "why?" Perhaps there is some sort of ideal that Ruby's author Matz and the author are both striving for. If Ruby is closer to the ideal than PHP, an observer who examines the two languages and this work may wonder if the intention is to make PHP programming more like Ruby programming, where is actuality the author is seeking the ideal. This trick of perspective probably explains why so many people seem to reinvent Lisp over the years. They aren't trying to turn their languages into Lisp, they're seeking what Lisp's authors have sought over the years but haven't found another path towards the ideal. As to why... PHP and Javascript are both very interesting because they are so widely deployed by default and have a very large "market penetration" amongst programmers. Once you get past the "uncool factor," reaching PHP programmers is probably one of the highest value activities a toolsmith can pursue. JM2C.
- dhotson 16y agoThanks for taking a look Reg. Would you mind if I ran an idea past you? The other idea I'm toying with is something similar, but having immutable classes. So basically every time you add a new method, instead of modifying the class you would return a brand new class. Crazy? :-)
- leftnode 16y agoI like it. I wasn't aware you could have function names with the same name as a keyword. Ninja edit: appears you can't, it has to be an undefined method caught with __call(). Neat.
- evilmushroom 16y agoComponent based object oriented-ish ala Ruby?
- wvenable 16y agoIt's an interesting idea but it adds a lot of indirection and extra code for every method call. I'd like to see some benchmarks, but my guess is that this technique slows things down a lot.
- Nycto 16y agoI had the exact same thought. PHP is already dog slow... that's one of it's trade offs. An extra layer of indirection would kill it. Also, you are losing a few important feature of PHP by doing this: - Type hinted arguments (http://www.php.net/manual/en/language.oop5.typehinting.php http://www.php.net/manual/en/language.oop5.typehinting.php) - Static method calls - Autoloading And there are a bunch of things that would become nightmarish to manage. For example, object serialization or cloning.
- dhotson 16y agoI'm sure the speed is awful. But that's not the point. Also, type hinting isn't all it's cracked up to be in PHP. In my opinion, type hints don't fit well in a dynamic language like PHP. The main problem I have is that you can't pass a decorator object to type hinted methods. Also, it makes it harder to pass mock objects for test cases. Static method calls are possible, you can just add and call methods on the class object. Autoloading probably isn't too hard to figure out. You make a good point about serialisation. Closures can't be serialised. To be fair though, you can't do it in Javascript either.
- Nycto 16y ago> I'm sure the speed is awful. But that's not the point. Fair enough. I can appreciate that you have done an interesting bit of hackery here. Along that vein, you might consider adding support for uniform access. With this, methods without arguments can be called as properties, and properties could be exposed as read/write or read-only. > Also, type hinting isn't all it's cracked up to be in PHP. This is a matter of taste, but I completely disagree. In my opinion, type hinting is one of the redeeming qualities of the language. It means that PHP is taking care of the sanity checks for you. Of course, I'm partial to strongly typed languages. > The main problem I have is that you can't pass a decorator object to type hinted methods. Polymorphism allows you to do this. In the context of decorators, that usually means defining an interface.
- jbm 16y agoYou'd think after five years in php I would have picked up on that nifty method to chain methods together. (referring to : function Obj($methods=array()) ) Awesome hack dude.
- dhotson 16y agoThanks. :-) Although, usually I go for a static create() method instead of a function. Oh man, I really wish they'd make constructors chainable. :-(
- bluesnowmonkey 16y agoSteve McConnell would like a word concerning non-idiomatic programming.
- raganwald 16y agoI have no objection to Mr. McConnell joining us, having his say, and explaining why his maxims are a greater good in the domain of PHP programming. Until then, perhaps you may want to make a point that you feel doesn't need an Argument from Authority? http://en.wikipedia.org/wiki/Argument_from_authority http://en.wikipedia.org/wiki/Argument_from_authority
- bluesnowmonkey 16y agoIt was kind of a joke.
- raganwald 16y agoExcellent! have another upmod for catching me out as a cranky old man :-)
- bluesnowmonkey 16y agoHey it happens to the best of us.
- apgwoz 16y agoAt first glance I was thinking this was prototypal, but upon a second look, it doesn't seem to be. It does however, seem like this approach would make it easy to get prototypal inheritance, no?
- olalonde 16y agoAs someone who has been coding PHP for over 5 years, I don't see any obvious benefit of doing OOP this way in PHP except for making your code look clever. Any explanation would be appreciated.