How can I represent a formula in a way that is understood by both Javascript and PHP?
I'm writing a shopping cart in PHP, and part of its function is to calculate shipping for a given set of products. For users with Javascript enabled, I want to provide the smoothest experience possible by having an accurate shipping calculation done on the client side so that changes in the cart do not require page reloads or AJAX calls.
Currently I have a function in PHP that calculates the shipping charge, and another function in Javascript that mirrors its functionality. I'm concerned that the formula may change frequently, and having to rewrite two separate functions in two different languages (and make sure their outputs match) could create maintainability problems.
What I'm looking for is a way of representing the formula for calculating shipping charges in some language agnostic way, so that it can be interpreted by both PHP and Javascript. Currently the shipping formu开发者_如何学Gola is only a function of a single variable (number of items) although I would like the capability to add more without too much rewriting.
Is there an accepted way to represent a fairly simple mathematical formula in a way that can be interpreted by both PHP and Javascript? Preferably without writing my own interpreter?
Edit: I don't need a whole function, just a fairly simple mathematical formula along the lines of "4 + 1.5n". The kind of thing you would find in a typical spreadsheet cell.
Generally, you are best off just to maintain two versions of the code. The most elegant solution, of course, is to write the server logic in JavaScript itself (see cappuccino.org); this would be a poor choice for you because it sounds like you have already written a ton of PHP code.
However, if you really feel the need to scratch this itch, consider an AJAX callback to the server. While slightly slower, most users will not notice.
I would say do it on server side, and user AJAX, It won't make much of difference for user, but maintaining two versions can make a difference when user after submitting the order sees the different calculation.
Anyway if you due to some reason do not want AJAX at all, best way would be to have single javascript library which is executed both at client and server side. You may execute javascript from php on server side. e.g. you may use following from linuxto execute javascript
http://www.mozilla.org/js/spidermonkey/ http://www.mozilla.org/rhino/ http://www.wxjavascript.net/
see http://en.wikipedia.org/wiki/Server-side_JavaScript for more options
This is a bad idea, and will become a worse idea the more complex the calculation becomes.
That being said, Javascript does support the dollar sign as legal in variable names. There's enough common syntax between the two languages that you could easily write code that parses and works the same in each, as long as you're only dealing with simple math. Arrays may be a bit tricker. Check out PHP.JS, a set of libraries to mimick PHP builtin functions in Javascript.
(Edit: I edited in the link to php.js without knowing someone else was going to post the same thing during my edit. Credit / horrors to him. ;) )
Maybe. Take a look at php.js. It gives you all the php functions but in JavaScript.
You could also write the whole validation in php, and use Ajax to query on the client side.
One would say this is the time to add some automated tests to your application, and run them at least each time you want to deploy your application to your production server -- and if some of those fail, cancel the deployment.
You'd have tests both for PHP and JS code, and some of those tests would calculate some total amount / charges / shipping costs ; and if something goes wrong because of some code change, you'd be able to detect the problem automatically, without your application breaking in production.
Of course, it requires a bit more work (both to write the tests, configure the building platform, and maintain the test data) ; but that would be a great security...
Another possible solution would be to write your calculation code only in Javascript, and run it both on the client side (this is not the hard part, obviously), and on the server side.
From PHP, you can execute JS code using the Spidermonkey PECL extension (Note it's still in beta, though ; and you'll need to be able to install PHP extensions, which will probably be possible only if you are admin of your server -- and not sure about stability).
Here is an article about it : Using JavaScript in PHP with PECL and SpiderMonkey.
With this solution, you could use JS for the code that only runs on the client ; PHP for the code that only run on the server... And JS for the code that runs on both sides.
精彩评论