Today in the official CakePHP google group I proclaimed with poise that having PHP helpers for outputting JS (like CakePHP’s JS and Ajax helpers) is a very bad idea. And then someone from the group asked me if I have any argument to defend that. Here are the points that I posted there:
- It minimizes the code but maximizes the chance of error
- It really becomes hard to debug
- We should write unobtrusive Javascript.
- In almost 99% cases we need to write custom JS logics. Unfortunately we can’t write it using PHP helpers.
- Most of the PHP devs know or should know how to write Javascript, at least what those helpers can do - it's easy man!
- JS itself is not that hard that, any PHP dev should be able learn it quickly.
- JS libraries are very easy to learn as well.
- It becomes hard to manage/modify or extend.
- If you really want to use cool JS stuffs and you think you cant - then I would recommend you to hire a JS Ninja or become one. It will be far better than writing PHP to output JS.
- JS libraries are there to minimize your effort, maximize reusability.... Why do we need to use PHP?
- Less control, less flexibility..... and we are getting almost nothing good in return.
- Moreover, it can make newbies confused, they can think that this is the right way of doing JS and waste their precious time learning it and trying solving problems using it. It is better to put in that time to learn JS itself.
Is there anyone out there who can justify using PHP to output Javacript? I did not find any real benefit of that, and to me it’s really really a weird idea! CakePHP should abandon their Ajax helper and some part of their JS helper. The only benefit that I can see of having those helpers is we can then call CakePHP an ‘Ajax framework’! Well to me it sucks even more, it sucks big time!