PHP Object Oriented Programming Reinvented
dhotson.tumblr.com
dhotson.tumblr.com
Is the intention here to make PHP more Ruby like? If so 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.
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? :-)
Yep, the intention was to implement classes in a more dynamic and hackable way. Ruby was certainly an inspiration, but I'm not really trying to copy it if that's what you mean.
Ruby is pretty good in this regard, except that classes are global. This means that any change you make to a class is permanent (it's not scoped).
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)
- Static method calls
- Autoloading
And there are a bunch of things that would become nightmarish to manage. For example, object serialization or cloning.
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.
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.
This doesn't work if you have a generic decorator class. You can't really make it implement all the interfaces you potentially want to use.
It's the same problem with mock objects in test cases. You can't pass them because the type check prevents it, even though in practice everything would work just fine.
See Joose, Moose, and Cappucino for other examples.
(referring to :
function Obj($methods=array()) )
Awesome hack dude.
Although, usually I go for a static create() method instead of a function.
Oh man, I really wish they'd make constructors chainable. :-(
Ninja edit: appears you can't, it has to be an undefined method caught with __call(). Neat.
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...
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".