PHP will never have a (real) Rails-like framework
I know the title is harsh but it's so true. At least it is according to nowadays php's implementation of classes and objects, which do not permit to "reopen" and add or redefine new methods to an existing class definition, which is the basis upon Rails (and I presume lots more of applications and frameworks) is built.
This ruby feature managed to freak me out when I first read about it. Redefining an existing class? Who would think of it as a good idea? Where is the maintainability and the good practices of object oriented programming? How can we rely on some class behaving as expected if some piece of code is changing its internal behaviour?
But then I got the answer - when used within reason, this allows you to extend existing classes. Extend. Morph. Adapt. Improve. Refine. Lots of concepts which started jumping around me and made me understand all in a sudden how Rails plugins worked so seamlessly, without having to do any extra include, or without having to touch Rails very core files, or defining hooks at certain places. If you don't like some aspect of Active Record, you could write a plugin which overwrites that behaviour - only that one. The rest remains the same.
The aesthetics is somehow shocking for php-only programmers. It's something like this:
class A
function hello_world
puts "I'm hello_world"
end
Any instance of A running hello_world will output "I'm hello_world"
But if you add this anywhere after the definition of A
class A
function hello_world
puts "I'm hello_world v2"
end
and run hello_world again, you will get "I'm hello_world v2". Which definitely is impossible to do in PHP - you would get a Fatal error: Cannot redeclare class error as soon as you tried to "reopen" a class and add or redefine some methods.
While this is not a problem for the average use of php, it turns to be the opposite when you want to do something smart with php5's new and shiny objects model. Something like, for example, building a cool framework like Rails.
There have been some attempts already, like for example CakePHP or Symfony. I just have experience with CakePHP and while it is magnifically built (given that it maintains compatibility with php4 and php5) it will never reach the whole expressivity and power that Rails has.
A quick example is the AppModel class. AppModel is the CakePHP's equivalent of ActiveRecord::base. In Rails, when you declare a model you just extend ActiveRecord. Simple as that; you don't need extra stuff in the middle. In CakePHP you need to extend AppModel, and if you want to modify AppModel in your application, you have to write a new AppModel.php - which cake will load instead of the default, empty one. Yes, AppModel is an empty class whose sole purpose is extending Model (which has the real ActiveRecord-like methods). That doesn't sound very flexible.
So at the end, AppModel is not more than a simple intermediate step for overcoming the limitation of php's inability to redefine the underlying class, that is, Model. We need to add an extra class in the middle for each level that you anticipate the user of your classes will want to redefine or extend.
Meanwhile, in Rails you would just add some code in the plugins directory with the new functions for ActiveRecord, and your models would still extend ActiveRecord::base. No extra levels of hierarchy whatsoever.
Another interesting example is the famous acts_as_taggable plugin, which allows programmers to add the ability to tag items (i.e. ActiveRecord models) just by adding a single line of code to the model (acts_as_taggable), using ruby's ability to reopen classes and add a series of new methods to existing models.
Even more, there are some Ruby core classes, like Date, to which Rails adds new methods, while they still belong to the Date object, not an artificial AppDate intermediate object in the middle, and without modifying Ruby's core files. Isn't it beatiful?
And before you discard my arguments as biased towards Rails, there are more languages which allow for classes to be reopened, like Javascript (and I believe ActionScript was like that before, don't know about ActionScript 3). These flexible languages have allowed people to write such amazing stuff as Prototype or Script.aculo.us, by making use of the redefinition and extension of existing classes.
So that's why I say that PHP is not flexible and will never have a real Rails-like framework :-)
Hope it's clearer now…


herotyc
20070127
Interesting article. I've developed an application using Cakephp, it was very easy, but it's not flexible, when you need to change some(not so common) behavior you've to start messing with code you've not written.
I'm using symfony for a new app I'm doing. It's more flexible(php5 is better than php4), easy to debug, AJAX is perfectly handled(this one is the point), you can build/change your database structure without using any SQL. In the other hand, I hate the php-like templates(views), it makes your html code look ugly; the .rhtml ruby way is cleaner.
Cheers.
sole
20070130
How do you debug? that's something which also annoys me in php, that need to do echo's everywhere in order to debug :-/
I remember I read long ago about opening a port in your machine to enable for remote debugging but I haven't heard of anyone doing it.
PHPDeveloper.org
20070131
Soledad Pendaes' Blog: PHP will never have a (real) Rails-like framework…
…
fluminis
20070131
Well, I never used Rail so I did not use such functionnality of re-define class.
I develop a lot in C#, java and php. And I think that if the framework you use is well design, you don't have to redefine classes. or if so, working with interface and factory pattern would be enough, isn't it ?
sole
20070131
What I'm saying is that you can _emulate_ Rails in php but you can't have a _real_ Rails in php, because of the limitations php has in what regards to objects.
Ricky Clarkson
20070131
I don't use PHP and I don't use Rails*, but you can't reopen a class in many languages. Lisp is an object-orientated language**, and the methods don't belong to the class. Effectively when you call a generic method, the actual method run is chosen at runtime based on the types of the arguments.
E.g., blah.toString() (Java) is written as (to-string blah), and the type of blah is analysed, so that the right to-string method can be used. In this way, you don't need to be able to reopen classes (although you can, if you want to add more fields), and I think Rails could be implemented in this way.
* I've used both, but not extensively.
** Lisp can do OO according to Alan Kay, the person who invented the term 'object oriented'. It's not the only paradigm that Lisp supports.
kongmeng
20070131
You only compare PHP and Ruby, I'd like to hear what your thoughts are on Python. Python, TurboGears, and SQLAlchemy make a pretty powerful framework for web development that one might find to be a better alternative to Ruby on Rails.
sole
20070131
Ricky, I haven't used Lisp but I *think* that's not how ruby works, as far as I know.
kongmeng, I can't comment on Python because I haven't managed to use it yet :-)
I just talk about what I know.
Jim Plush
20070131
"How do you debug? that’s something which also annoys me in php, that need to do echo’s everywhere in order to debug :-/"
If you don't know how to debug in PHP then I'm skeptical about your abilities to talk about PHP in a serious way. I haven't used echo statements in years to debug, nor var dumps. There are plenty of debugging options for PHP that act just like a c++ debugger.
sole
20070131
I'll be more than happy to hear about some practical and effective debugging methods that you have used. Until now, I haven't been able to find anything which is effective, practical and that can be applied to the environments I have been working in. May I have to stay skeptical about that as well or can you provide some insightful data about those thousands of debugging options?
In any case, that doesn't prove anything against the fact that you can't have a real rails-like framework in php.
Mike Cantelon
20070131
True. PHP is not Ruby and Ruby is not PHP. Different tools for different uses.
Mike Seth
20070131
You are entirely correct, yet being correct on this point is useless. Rails is universally perceived as the verge of technological capacity and nearly universal answer to the most intricate business problems. That is wrong. Rails reintroduces fun back into coding. Rails is probably as close to getting head as software development can get. And yet, many people do not use Rails - at all. Rails is not designed to wrap around every kind of application and it is not even suited well for some types of web applications. Ruby is still a seldom guest in the web world. If you are writing a new project that uses web services to talk to other systems and has pretty rounded corners then Rails may be helpful. In my case however there is more than one instance of a project where Rails simply wont fit. For example, I have a legacy accounting system written in PHP 3 by a person so lazy and clueless that the whole thing is a huge mix of code, HTML, javascript and MySQL API calls. The system is in production. It can not be stopped and it would take months to rewrite and test it. What can Rails do for me here? I need to fix bugs and add functionality now. I need to have all reports produce xml. Right now they are implemented as a furball of switch, print and mysql_fetch_assoc statements. All this needs to be done with extreme care. Sometimes, rewriting from scratch is not an acceptable answer. Even if it was, there is still too much magic in Rails. The core code is all but incomprehensible for people not versed with Rails core technology. Ruby is an extremely powerful platform, which is why it should not be trusted to noobs. I have already seen people read all Rails books and tutorials they could lay their hands on. They would then proceed to load a custom html helper using a require call in their Erb templates. The helper itself would be a subclass of Proc that duplicates some html generation functionality from Rails' standard library. My point is that an experienced PHP monkey would perform better than a noob on Rails. The technology is not dramatically different. Rails is no silver bullet so stop praising it as if it was.
Curt Hibbs
20070131
The best indication that PHP will never have a (real) Rails-like framework, is the simple fact that David Heinemeier Hansson (the creator of Ruby on Rails) originally tried to create this framework in PHP, but gave up in frustration and switched to Ruby.
sole
20070131
Curt, exactly.
But I have been asked like, let's say, 10 times recently about "what do you think it's the biggest difference between php and ruby? why can't we just use php? what's the deal with rails?" and I thought it could be interesting to let people know what's going on with this.
I'm not saying that we all should remove all instances of php from Earth, it's just that I find futile to keep trying to create workarounds for emulating what it's impossible to do - at least in php5.
And php will have the insane amount of share it has for years, or until something like mod_ruby (but superoptimized) becomes a mundane feature in every server.
AlmostAlive
20070131
PHP5 does have the __get, __set, and __call pseudo methods which, while they don't allow you to modify a class, do allow you to dynamically handle properties and methods. I assume that this would allow one to build a true Rails-like library in PHP.
sole
20070131
No, not really. That would imply modifying the classes and also having to think in advance of all the possible method calls that the class could receive, and implement that functionality. So it's not exactly the same.
AlmostAlive
20070131
No, sole, you wouldn't have to think in advance of all the possible method class. I have a project right now where objects have methods and properties based on the values contained in an XML file. Load the XML file and at runtime the objects have certain methods and properties. This is all done with the __get, __set, and __call methods in PHP.
It would certainly be possible to build a system in PHP where methods can be dynamically added and removed from classes. It wouldn't be as pretty as Ruby, I'd require some work, but it could be done.
sole
20070201
Hmmm, I can't really figure out how to do that. While I can imagine how to create new properties for an object, I can't think of a way of creating methods on the fly.
Can you show an example?
In any case, adding code to an XML file is not what I call easy extensibility. Let's not abuse the technology ;-)
SantosJ
20070201
This is an interesting concept, redefining a class that was already defined. I would shout, "Insanity!" The thought of someone having the nerve to think that they can rewrite my class. Hell, there might even be another framework with that class that decided that they would call their class by my class name!
The reason why there can never be redefined classes in PHP, nor any other sane OOP language is that you are allowed to _extend_ classes. There are friend classes in C++ that does basically a similar action in a different fashion, however you still can't redeclare the class name.
There are many good reasons for why you should not be able to redeclare an existing class name. Mostly because in PHP you can include the same file multiple times and it doesn't make sense from a speed and compiling stand point to compile the same class more than once. The second case is that the class wouldn't extend the class (that method has already been established by another code syntax), it would overwrite it, which from the technical standpoint wouldn't be acceptable since one developer might be using the methods in the first class and it would fail because the methods might not exist in the second class.
I doubt that the Rails framework couldn't be done in PHP, albeit in roundabout way. To master a language, you have to use it the way it wants to be used to accomplished the same task.
For another example, it is possible to do OOP in C (also in a roundabout way), but it is _better_ to do it in C++ which natively supports the syntax. You are basically saying PHP is C when it comes to friend classes and Rails is C++ (which does support friend class concept but not in the way Rails does it).
To be perfectly honest, I think your example could be rewritten as:
class A_Base
{
function hello_world()
{
echo “I’m hello_world”;
}
}
class A extends A_Base
{
function hello_world()
{
echo "I'm hello_world() 2";
}
}
Internally, it is the same thing from what I can gather from your code and explaination. I haven't messed with the seductress yet, but I am planning on it.
AlmostAlive
20070201
"Can you show an example?"
Here's a really simple and stupid example but it shows the possibilities:
class test {
public $contents = 'hello';
function __call($name, $args) {
$function = get_class($this).'_'.$name;
if (!function_exists($function)) throw new Exception('No method');
array_unshift($this, $args);
call_user_func_array($function, $args);
}
In this case, I'm dispatching method calls to functions by name but I could dispatch to methods of other objects. I could find numerous other ways of doing the same thing with more flexibility, if necessary. The above example would be used as follows:
function test_print($self, $more) {
echo $self->contents.' '.$more;
}
$test = new test();
$test->print('sole');
The output of this would be "hello sole";
"In any case, adding code to an XML file is not what I call easy extensibility. Let’s not abuse the technology"
Well there isn't any code in the XML — just data and configuration.
pcdinh
20070201
I think that flexibility always comes at a price, sometimes, at an expensive cost. A person can love the flexibility of Ruby but when you try to apply it in a team development process, it can become a true nightmare. You must choose between the discipline and the freedom.
I think that PHP has enough discipline and freedom and in fact, it will never lead us to worry about the outcome. Moreover, Ruby on Rails is a start for a new way of thinking in web development. The later PHP frameworks are learning from its shortcomings. In the near future, what you will see is that fact that Ruby on Rails is just another framework. Please have a look at http://solarphp.com or http://framework.zend.com and ask yourself: Is Ruby on Rails mature? Is Ruby on Rails unique and the best? No, I don't think so.
sole
20070201
Santos: you haven't understood my idea. What I am saying is that those constructs you're showing to me are unnecessary in ruby, and thanks to that, Rails is possible. CakePHP is built that way, creating intermediate classes, as you can find in my article.
AlmostAlive: Thanks! but that's nothing new :-( I was expecting some kind of magical, undocumented 'eval' in the middle of the code which made possible to incorporate new code into an existing class.
And in a way, I feel a bit uncomfortable about having class methods not inside the class itself.
pcdinh: I haven't said Rails is the best thing ever. I have tested Zend and it didn't convince me. Too many workarounds, once again (as with CakePhp). So Rails is the one I am most inclined to use.
Oh and problems in a team development process can appear whether you use php, ruby or whatever. You need to define your process and your guidelines before starting to code, otherwise a nice and scary nightmare will be knocking at your front door.
MatteKarla
20070201
Look at php on trax. As soon as you start using php5 you can do a lot of stuff you couldn't in php4.
I think if David Heinemeier Hansson would have started in php5 we wouldn't have rails today.
However Ruby is an amazing language, and scripting small console-stuff in Ruby and ActiveRecord is really neat!
SantosJ
20070201
It still seems fascinating, but how does the interpreter or user know that their class name wasn't supposed to be something else. If they are a novice and choose to import all and that library has the same class name. In PHP it would fatal out, as it should, but you are saying that instead, the novice's class would extend the library class and hope that there weren't any methods in the novice's class that were the same in the library's class.
What I'm trying to say, is that it is a bad idea and can lead to really difficult to find bugs. If the coder knows that the class exists and knows what methods exist, then he doesn't have to worry about losing a defined method.
Wouldn't it also kind of negate the private visibility (or does Ruby not have that?) since the new class is basically the same as the old class? There are just so many things that I wouldn't want the user to do.
It would take me a lot more before I could accept this way of doing things over the well defined way that PHP, C++, and Java does it (when it comes to this discussion).
What you are saying is the "roundabout way" or doing things is actually the right way in PHP and other languages. Ruby in fact goes against the norm and creates something that completely changes how classes are extended. I don't see how it doesn't lead to mass shooting of the feet in the coding environment.
AlmostAlive
20070201
"I was expecting some kind of magical, undocumented ‘eval’ in the middle of the code which made possible to incorporate new code into an existing class."
Your premise was that PHP couldn't do what Ruby on Rails does because it's not extendable enough. But PHP is clearly extendable enough. CakePHP is not the best example because it wants to be compatible with PHP4. But CakePHP != PHP and the entire premise of your article is false.
"And in a way, I feel a bit uncomfortable about having class methods not inside the class itself."
I wouldn't be comfortable with actually extending a class outside of the class. It breaks all the rules of encapsulation. PHP can easily accomplish the goals of Rails with __call with some extra measure of defined safety.
sole
20070201
Santos, it also surprised me when I first saw it, as I stated up there at the beginning of the article. But if you think outside the box and start accepting different ways of programming (within a certain order, of course) things get interesting. And there's not a single "right way".
AlmostAlive: and I still will maintain my premise. All the proposed solutions (using __call, __set, etc, and extending classes and classes) just lead to frameworks which try to be like Rails but won't reach their goal.
This discussion is going to last undefinitely until people stop defending php like crazy and start having a look at ruby and rails.
AlmostAlive
20070201
"just lead to frameworks which try to be like Rails but won’t reach their goal."
Ok, defend that position then. Explain in some kind of detail why it won't reach their goal. I'm not really trying to be argumentative… I'm actually quite curious. Obviously there are lots of features that Ruby has (first-class functions, for example) that make all kinds of constructs impossible in PHP. But we're talking specifically about Rails — and I think a vast majority (but perhaps not all) of all it's automagical capabilities could be done in PHP.
dani
20070201
have you tried this framework? -> code igniter
sole
20070201
Heh, I like curious people :-)
I think you answered yourself, "a vast majority but perhaps not all". It's on the missing automagic (both from ruby and from rails) that one can feel the pain!
Some things that come to mind:
- Ruby's Iterators. Ok, there is the php5 iterators stuff, but it doesn't feel by no means as simple as ruby's one.
- Ruby blocks (related to the above) Yield doesn't exist in php, does it? there's lots of automagic in rails coming from yield, if I'm not wrong. (more about yield).
- Not being able to reopen an existing class in php (again). I don't want to add code like the one you showed in order to be ready for a call to an undefined method -specially if I want to improve or add a new method to a precompiled 'core' class-. While this may be , it actually makes a lot of sense because you do not end up adding artificial classes which extend the base ones. An example could be adding a new method to Date. Do we need to create a Date_Improved class which extends Date? doesn't it make more sense to hold all the methods together? (No, as php/traditional programmers this sounds like an heresy, I know).
It doesn't look really useful with a simple Date example, but this ability allows for having plugins hooked to classes in a nice way - the least obtrusive way I have found to date.
This requires being organised and knowing where you place your code. Which means this requires you to be a serious developer.
- ruby mixins are kind of related to the above, and also allow for including code in a class in an explicit way (that would be like your example, but with the code being encapsulated in a Module, and not left on their own as a list of functions :-)). This code can be shared between multiple classes as well, by importing it in all the classes you need.
(and I'd better not moan about the extra ()'s and ;'s and {}'s in php!)
I would love to see a truly comparable, working php on rails, since it's way easier to commercially host php than ruby. But given these limitations, I feel that it's not going to happen anytime soon.
And I would love even more to be wrong :-D
sole
20070201
dani (I translated your comment to english for coherence): I have been looking at the documentation. It has very nice ideas but I don't think it's a "php on rails". Sorry!
AlmostAlive
20070202
I've developed some pretty wild stuff in PHP… If I had more time I think I'd take up the challenge of building a true rails-like library in PHP. That'd probably be the only way to know if it's truly not possible (or not practical).
sole
20070202
Maybe we should start one of those pages where people donate money in order to get someone to build something! Like the "money for the first one which boots up mac os x in a pc" challenge :-)
Pedro
20070202
Has anyone tried the Akelos Framework?
Though still poorly documented, it seems to closely follow the Rails approach (with the obvious language limitations) in PHP. I've tried it a bit, and it seemed promising.
sole
20070202
"still poorly documented" and "with the obvious language limitations". Doesn't sound very attractive.
Even though, I'm not looking for more php frameworks. Or do you want to show it as an example of php framework which can't be like rails because of php? ;-)
**** FUNKABLOG 64 BASIC V2 **** » Blog Archive » You can't do Rails with PHP, and it's time to get over it
20070204
[...] Soledad Penadés says "PHP will never have a (real) Rails-like framework." She's right. And people need to quit getting their panties in a bunch about this fact. I know the title is harsh but it’s so true. At least it is according to nowadays php’s implementation of classes and objects, which do not permit to “reopen” and add or redefine new methods to an existing class definition, which is the basis upon Rails (and I presume lots more of applications and frameworks) is built. [...]
rvr
20070205
I love scripting languages, particularly Python. Of course, Ruby is a better language than PHP. And Rails is a better platform to develop web apps, that's ok… unless you want to go the open source way and have wide distribution. Why? First, almost every single hosting service has just PHP support. Second, PHP is easier to understand, learn, hack and install. Probably, Zope fans said the same five years ago: PHP is a poor language and will never have a (real) Zope-like framework. However, did Zope greatness stop people from using PHP? Absolutely not. IMHO, Rails and Django will face the same problem in this context: technically better doesn't imply wide acceptance.
Matt Sharkey
20070212
I'm not sure how instructive it is to say that you can't make Ruby on Rails with PHP, since the framework succeeds primarily because it was built with Ruby. It's a bit like claiming that no boat will ever win the Indy 500.
If the question were, instead, can you build a competent MVC framework with PHP, then the answer is yes, definitely. We've already heard from evangelists for most existent PHP MVC frameworks, and there are scads of other homegrown solutions waiting to be advocated.
The real issues here are the shortcomings of PHP's object implementation, and the problem doesn't lie in the ability to extend its classes after they're defined. The real problem is how to extend PHP classes *during* definition. More specifically, the problem is the one-to-one inheritance system that PHP requires.
Ruby's mixins — and multiple inheritance in other languages — mean that a Model class can inherit methods from the HasChildModel class, as well as the HasAWifeModel class and the HasADogAndACatModel class. And the nature of mixins means that another Model class can HaveAWife and HaveAChild but not HaveADogAndACat, which is extremely convenient for Model classes that live in small city apartments. For this to work in PHP, however, all these classes have to be daisy-chained together in parent-child relationships, like an Old Testament verse: Model begets HasAWifeModel begets HasAChildModel begets HasADogAndACatModel, and lo unto the Model that wants pets without the hassle of getting married.
What's more, once you define these relationships, there's no backing out of the chain. If class A extends (inherits the properties and methods of) class B, which itself extends class C, there's no way to get an object of class A without getting the baggage that B and C add. Which is probably fine. But if the time should come that you'd like a class A that extends class D, then you're forced to write a whole new class definition of A, with all its method duplicated and with a brand new class name like AFromD, all just to bypass the mess you got yourself into by getting mixed up with those layabouts B and C. By the immutable laws of PHP, "A extends B" is writ in stone, nothing under D's green earth can tear it asunder.
What PHP needs is something like Ruby's mixins, but what it has instead is something called an *interface*, which a class may *implement* alongside other interfaces, but which offers only a blueprint for methods and properties a class must define on its own. If PHP interfaces were allowed to bring their own methods and properties to the party, then we could have a class A which gained the methods of class D without having to define them itself, and HasAWifeModel could finally gain the methods of HasADogAndACatModel without having to resort to becoming a HasAChildModel, which truth be told he's neither financially nor emotionally ready for.
Fork and Spoon Helmet » Blog Archive » PHP’s object inheritance problems
20070212
[...] Penadés says that "PHP will never have a (real) Rails-like framework" because the language does not allow classes to be modified after they've been defined. [...]
sole
20070212
Now that's a really interesting comment, good explanation of mixins! I don't think it would be the only reason for not being able to build rails in php. So I would add it to the bag of reasons :-)
About how instructive is to say that, well, it is to stop people from trying and insisting until exhaustion on building "authentic rails-like php frameworks". Or at least to be able to say "you liar!!! that's not a rails-like php framework! it's a MVC framework!"
Gábor Hojtsy
20070213
Seems like you have not heard of the runkit PHP extension:
http://php.net/runkit
Peter Czimmermann
20070213
I am pretty tired of having "Rails" mentioned as a language.
[quote]In Rails, when you declare a model you just extend ActiveRecord. Simple as that; you don’t need extra stuff in the middle.[/quote] is this a Rails feature? NO.
sole
20070213
Gabor: no, i haven't heard of the runkit stuff. And I am not sure I can find it really feasible. I for one don't see myself asking my hosting support for runkit. Maybe running a full dedicated server would be different, certainly.
Peter: please tell me at which point I have said that Rails is a language. Or maybe, read the article before starting to rant like that. Thanks!
Physon
20070214
It's realy wrong to have a (real) Rails-like framework in PHP. Just like fluminis said at 31 January 2007, 15:05:
Well, I never used Rail so I did not use such functionnality of re-define class.
I develop a lot in C#, java and php. And I think that if the framework you use is well design, you don’t have to redefine classes. or if so, working with interface and factory pattern would be enough, isn’t it ?
So, I doesn't need such functionnality, then why I have to have a (real) Rails-like framework?
sole
20070214
Physon, try Rails. Now go back to write any web app either with bare bones php or with a Rails-wannabe php framework. Then think about it.
Rails-Free Living at Random Strings
20070216
[...] I said awhile ago, and as others have said more recently … I feel compelled to say it [...]
Chris Hartjes
20070217
Soledad,
I just recently gave a talk at the Vancouver PHP Conference 2007 entitled "What Can PHP Learn From Ruby on Rails?" and I couldn't agree with you more. Once you build something with Rails you get to see the ugly truth about PHP's current limitations. This is not to say that you can build something in Rails you couldn't with PHP, so the lesson to be learned is one of ideas and practices, not Rails itself. I posted a screencast of my talk on my website if you're interested in checking it out.
I shudder to think of what kind of monstrosities would have been built in PHP if you if you had blocks and closures available to you…
Mr. Cheese
20070217
Ruby is a fun and PHP in comparison is quite ugly. Lots of PHP projects and frameworks look like absolute garbage when compared to Rails (Drupal anybody?).
However, aside from developing yet another social networking site with lots of cutesy script.aculo.us driven visual effects what do the rails otakus suggest RoR is good for?
Rails still sucks for Unicode support, ActiveRecord is a sad sad little pattern that doesn't work for any complex database schema, and Rails apps have horrible scalability. When figured that 75 servers would be needed to support a RoR app versus 50 servers to support an equivalent PHP5 app we figured that money does talk once you scale past what most RoR apps are used for.
Don't get me wrong, RoR is a great tool for some overpriced SF-Mission district contractor who floats between small startup gigs, because in such a situation fast eye-candy prototyping tool matters most (you're selling your wares to some marketing head). But if I'm developing webservices that needs to scale massively I'll take PHP5's amazing set of XML extensions, opcode cache options, great profiling tools, and the fact that code can be optimized into a native code extension.
Different tools for different sites.
Crux_op
20070223
There is something very wrong about being allowed to redeclare classes.
I'm a devoted PHP programmer for web-work, but for desktop functionality I code in C, C++, C#. In any C derived language, you are not allowed to redeclare your class.
In C and its derived languages, like in PHP, you can must extend from an base class. Then and only then may you overwrite functions inherited from the base class.
This way you always know what a class is doing. You'll never wonder "is it printing out X because of a bug in the function, or because somewhere in my tangle of code I redeclared this class?"
——–
Lastly, I'd like to add that its silly to expect a RoR framework designed in PHP. I think we should all give up looking for that and instead aim for a PHP framework designed for the PHP designer in mind—not the ruby designer who is forced to work in PHP.
sole
20070226
It's amazing how obsessively C/C++/C# developers advocate the use of traditional OOP programming and how mad they turn when they hear of the sole idea of redeclaring classes. It's even more amazing that they do that after reading the whole discussion, which is something that apparently you haven't done.
I really think you need to have an open mind when approaching new stuff and in general when approaching everything, specially if it can save you time or make things more flexible. That said, it would be nice if you read things before posting something which has been said above by strongly-commited-to-php developers like you.
Tomasz
20070301
Redefining classes - by that i mean adding or redefining class methods is as easy in PHP as in Ruby.
Just have to add PECLs: classkit or runkit.
I guess findig host for apps with classkit and runkit wouldn't be so hard if only PHP would use them more frequently, e.g. in nicely written PHP framework.
Seen Ruby hosting before Rails set off?
sole
20070305
yeah it was said before, some comments above.
I think classkit or runkit use will never be widespread because they don't come with any "uncompress and install" package. Most of the developers out there aren't willing to install modules or compile stuff, they just want to install and code, if you know what I mean.
Amir Laher
20070418
I think hose guys at PHP specifically designed PHP *not* to allow you to redefine classes. This is not because they're lazy or incompetent - more that they are commited to more traditional OO principles, as with C++/C# and Java. i.e. use 'polymorphism' instead.
The ultimate way to produce functionally similar classes in these languages, is to code to an interface:
Rather than overwrite a class, you can create factories to return instantiations of classes which implement your interface. In-so-doing you can be confident that you've got nicely de-coupled, predictable, extensible code.
See the Zend Framework for several examples of this approach - great stuff, and the first time I've seen this done earnestly in a PHP framework.
… in short, dont berate one language because it doesn't behave like another - it's probably got good reasons for being different!
sole
20070419
I personally don't care whether php is or not able to redefine classes, but if you want to build a Rails-like framework for php, in rails style, and which feels like Rails, you can't do that in PHP. And because I'm sick of repeating and explaining yet the same thing over and over again, I'm going to close the comments. Thanks!
Sylnsr
20070711
Sorry to be such a stinker on an old subject, but on the article "PHP will *never* have a (real) Rails-like framework" where you mention "At least it is according to nowadays php's implementation of classes and objects" (to me::) is the equivalent of saying something like "I can never be a day older than I will be before the day is over". ~ the day isn't over….
** Not really off topic :-) **
That being said, when someone comes up with a STANDARD for how MVC's; frameworks and CRUDs should work, look and feel, then I think the other programming camps will be able to co-exist (ya! right!). More than anything I think such fundamental things should be standardized so that a developer wanting to switch from say, Struts to Rails wont have a huge learning curve in figuring out a new framework.
In short I find it sad to see that there are virtually *never* any standards for such things …. um, yet.
So how do we get started on that?
sole
20070711
Alright, reopened the comments again. And I moved your comment to the right article :-)
Jon
20070715
If I follow you correctly, then what your describing is easily done in most object oriented langauges by inheriting the base class, and then declaring an override on a class method. Inside that class method you call the base method, and then any additional code.
Yes, PHP won't have it, because PHP is barely OO.
sole
20070715
No, that's not the issue here.
.:: ju ::. » links for 2007-08-08
20070808
[...] PHP will never have a (real) Rails-like framework - soledad penadés (tags: php ruby rails) [...]
John Benson
20070823
I'd like to throw out some flamebait by paraphrasing some of the discussion in terms of a COBOL-only programmer trying to get his head around C:
"In COBOL I can put a value in a variable and it stays put. But in C I may have to access a value through a pointer. So if I dereference the pointer and find a value I don't expect, I don't just go looking for code that wrote to a variable, I also have to worry about checking for code that might have written to the pointer and pointed it at the different value. Imagine the possibilities with multiple indirection. I really don't see how you can possibly debug with the ground shifting under your feet like this. Interesting though it may be, perhaps C is better reserved for experts…"
While bullets are flying over that, I'd like to point out that enthusiasm for Aspect Oriented Programming implicitly accepts tinkering with class definition after-the-fact. If there are any AO programmers out there, I like to pose the question:
"Is the Ruby/ROR acts_as_… capability just another way to smuggle in AOP? If so, is it cleaner than the AO bytecode injection implementation that seems so invasive to me?"
me
20071023
I hear a lot of people talk about how elegant ruby is, but elegance is an opinion not a fact. What is a fact is that ruby benches quite slower than python, perl, and yes even php, it's three major contenders.
php5 is the norm now. Stop using anti-php4 arguments.
Also, I'm waiting to see how maintainable ruby code turns out to be. Let's see what people are saying in a few years after code has passed hands a few times. My guess is because most ruby code will be rails, it won't be the mess a lot of noob php code is. Still, you can't fault php for that. I was writing MVC php years before rails existed. Rails did not invent MVC, it just brought that style to a new level of notice.
Modifying classes on the fly sounds like a horrible thing for code maintainability to me. Maybe I misunderstand what you mean by that, but the whole point of OOP is to provide a certain amount of constraint for the sake of organization.
Constraint & organization & maintainability vs performance & flexibility. I find a lot of the anti-php arguments say php fails at all of the above, when actually it has all of the above.
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=php&lang2=ruby
ruby is slower everywhere but startup (which doesn't matter in a web environment). These benchmarks don't even take into account the MAJOR improvements you get when you run an opcode cache such as APC or eaccelerator.
Try developing something lick flickr in ruby. Your hardware costs will be through the roof. Yeah they could have written flickr in python rather than php. If the python community were half as vocal as the rails zealots, I'd be much less annoyed. At least python performs, which is why I use it rather than ruby for non web related tasks such as cmd line tools…
sole
20071024
/me yawns at people which gets irritated when one enumerates php differences vs ruby and don't get the point of what I say
/me closes the comments, probably (for the second time)