如何避免在 Javascript 和 PHP 中重复公式?
我目前正在用 HTML5/Javascript 和 PHP 编写一个 Web 应用程序。
公司的一些员工将需要使用它来输入他们的工作日程。该应用程序将实时(使用Javascript)计算一些复杂的法律信息并将结果显示在页面底部。完成后,用户单击“保存”按钮,所有内容都会发送到数据库。
问题是我既需要用户实时查看输出,又需要管理人员从数据库中保存的内容获取相同的输出。我还需要支持用户禁用 Javascript 的情况。换句话说,我需要在 Javascript 和 PHP 中进行相同的计算。
让事情变得更复杂的是,该公式非常复杂并且会经常更改(大约每月),因此我想避免维护两个不同的版本。测试一个已经足够困难了。
另外我想避免 AJAX 向服务器询问输出,因为:
- 用户经常会根据实时计算的结果来构建时间表,所以即使是 1-2 秒的延迟也会让他很恼火
- 如果可能的话我必须支持HTML5的离线功能,这样用户就可以在手机上加载该应用程序,离线时填写其时间表,然后在线时上传它
目前我找到的唯一解决方案是用一种语言编写公式- 不可知的方式,然后使用一些方法将其转换为 PHP 代码和 JavaScript 代码,但这并不简单。
I'm currently writing a web application in HTML5/Javascript and PHP.
Some employees of the company will need to use it to enter their work schedule. The application will calculate in real time (using Javascript) some complicated legal infos and display the result at the bottom of the page. When he's done, the user clicks the "save" button and everything is sent to the database.
The problem is that I need both the user to see the output in real time and the managers to get the same output from what was saved in the database. I also need to support the case where the user has Javascript disabled. In other words, I need to do the same calculus in both Javascript and PHP.
To make things more complicated, the formula is very complex and will change often (every month or so), so I'd like to avoid maintaining two different versions. Testing one will already be hard enough.
Also I'd like to avoid AJAX to ask the server for the output, because:
- the user will often build its schedule according to the result of the real-time calculation, so even a 1-2 seconds lag could really be annoying for him
- if possible I have to support HTML5's offline features, so the user can load the app on his mobile phone, fill its schedule while offline, and then upload it when online
For the moment the only solution I found is to write the formula in a language-agnostic way, then use some way to turn it into PHP code and JavaScript code, but that's not simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你基本上有两种方法:
你可以编写一个独立于语言的公式(在数据库或配置文件中,因为它变化如此频繁)和两个代码生成器(Javascript、PHP)。这实际上并不像听起来那么可怕,只要您明智地选择输入格式并且您实际上指的是一个公式,而不是一些任意计算。
您可以使用服务器端 JavaScript,并在 JavaScript 中编写一次公式。维基百科有一个服务器端 JavaScript 实现的比较。甚至还有用 PHP 编写的 JavaScript 解释器,至少包括 phpjs 和 J4P5。
It sounds like you have essentially two approaches:
You can write a language-independent formula (in a database, or config file, since it changes so frequently) and two code generators (Javascript, PHP). This is actually less scary than it sounds, as long as you pick your input format sanely and you actually mean a formula, not some arbitrary computation.
You can use server-side JavaScript, and write the formula once in JavaScript. Wikipedia has a comparrison of server-side JavaScript implementations. There are even JavaScript interpreters written in PHP, including at least phpjs and J4P5.
当提到“实时”一词时,首先想到的并不是使用与语言无关的存储技术和解释性转换来持久保存数据。设计无限可扩展且易于维护的软件是很有可能的,但通常更具挑战性和耗时。
就我个人而言,我权衡与每个已知解决方案相关的成本与收益,并决定一种首先有利于最终用户并满足或超过关键目标可交付成果的方法。一旦我做出了满足主要利益相关者期望的决定,我的工作就是在这些期望的范围内尽可能设计和开发最具可扩展性和可维护性的解决方案。
根据您提供的详细信息,我绝对会使用本机 JavaScript 和 PHP 编写方程式以获得最佳性能。如果软件规范从字面上(不是象征性的)表示实时通信的必要性,那么我将通过实现WebSockets,否则长拉就足够了。
请注意:并非所有 Web 浏览器都支持 WebSocket,这可能要求最终用户遵守 支持的网络浏览器。通过使用诸如 Socket.IO 之类的工具,采用无缝客户端故障转移(或完全),可以避免此难题。
Persisting data with language agnostic storage techniques and interpretative conversions are not the first thoughts that come to mind when the term "real-time" is mentioned. It is very possible, but usually much more challenging and time consuming to design infinitely scalable and easily maintainable software.
Personally, I weight the cost versus benefit associated with every known solution and decide on an approach that, first and foremost, benefits the end-user and meets or exceeds key objective deliverables. Once I've made a decision that satisfies major stakeholder's expectations, it is then my job to design and develop the most scalable and maintainable solution as possible within the confines of these expectations.
Based on the details you've provided I would absolutely write the equations using native JavaScript and PHP for optimal performance. If the software specifications literally (not figuratively) denote the necessity for real-time communications, I would then provide this by implementing WebSockets, otherwise long pulling would suffice.
Please note: Not all Web browsers support WebSockets, which may require end-users to conform to a supported Web browser. It is possible to avoid this conundrum by employing a seamless client-side failover (or altogether) by using something like Socket.IO.
今天早上我想到尝试使用 XSLT 进行计算。这真的很有趣,因为它受到 Javascript 和 PHP 的支持,它是可靠的(与我必须自己编写的代码生成器相反),而且很合适,因为输入数据已经是 XML 格式了。
示例输入数据:
示例 XSLT(用心编写,可能不起作用):
我将尝试使用 XSLT 编写所有内容,如果我设法做到这一点,我将保留此解决方案。
This morning I got the idea to try XSLT for the calculation. This is really interesting, because it is supported by both Javascript and PHP, it's something reliable (contrary to a code generator I would have to write myself), and appropriate because the input data was already in XML.
Example input data :
Example XSLT (written by heart, may not work) :
I'll try to code everything with XSLT, and I'll keep this solution if I manage to do that.