如何处理不同版本的PHP
我正在编写一个 Wordpress 插件,因此它将由不同的用户和不同的 PHP 版本使用。问题是我发现某些函数(如 json_encode)在 PHP 5.3 中可用,但在 PHP 5.2 或更低版本中不可用。这会产生一个大问题,因为大多数用户没有最新版本。
我现在想要在插件完成 99% 后,
- 使用某种应用程序测试我的代码。我可以在其中放置最低 PHP 版本。那个应用程序。或者程序会找出像 json_encode 这样的函数。不确定这是否可能,但可能会解决我的大部分问题。
- 是否可以获取 PHP 中 PHP 函数的本机代码。我不确定它们是否是用 PHP 编写的,如果是的话我在哪里可以得到它们。如果没有,寻找替代这些功能的最佳选择是什么。当然,我不想从头开始重新编码它们
- 实现这些功能的最佳方法是什么。我发现一些开发人员检查 PHP 版本,而其他开发人员则检查函数是否存在。哪一个最好,为什么?
还想了解您的部署策略以及您如何处理该特定问题。
I'm writing a Wordpress plugin, so it's going to be used by different users and different PHP versions. The problem is that I found that some of the functions (like json_encode) are available in PHP 5.3 and not in PHP 5.2 or less. This creates a big issue, as most users don't have the latest version.
I want now, after getting the plugin 99% done, to do the following
- Test my code with some kind of an app. where I can put the minimum PHP version. That App. or program would find out functions like json_encode. Not sure if that is possible, but would probably solve most of my problem.
- Is it possible to get the native code of the PHP functions in PHP. I'm not sure if they are written in PHP or not, if so where can I get them. If not, what's the best option to find replacement for these functions. Certainly, I don't want to be re-coding them from scratch
- What's the best methodology to implement the functions. I found some developers that check for the PHP version, while others check if the functions exist. Which one is best and why?
Would love also to read about your deployment strategies and how you dealt with that particular problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
2.不。它们是由用 C 编写的编译扩展提供的。对于 json_encode (顺便说一句,自 5.2 开始可用,而不是 5.3),您可以使用 Zend_Json 作为替代方案
3.如果你想 100% 确定,检查函数是否存在是最好的。毕竟,人们可以运行 PHP 的自编译版本,但并非所有核心功能都可用。检查 PHP 版本号,了解名称空间、异常链等功能是否可用。
2.Nope. They're provided by compiled extensions written in C. For json_encode (which BTW is available since 5.2 not 5.3) you can use Zend_Json as an alternative
3.Checking if function exist is the best if you want to be 100% sure. After all, one can be running a self compiled version of PHP with not all core functions available. Check PHP version number, to know if features like namespaces, exception chaining etc are available.
您可以使用 PEAR 的
PHP_Compat
为旧版本的 PHP 提供缺少的功能和PHP_CompatInfo
找出最低版本和所需的扩展要运行的代码段如果您想提供自己的函数或类的用户态实现,最好将它们包装到
function_exists
或class_exists
块中,这样它们就不会运行干扰提供这些方法的 PHP 版本。You can use PEAR's
PHP_Compat
to provide missing functionality for older versions of PHP andPHP_CompatInfo
to find out the minimum version and the extensions required for a piece of code to runIf you want to provide your own userland implementations of functions or classes, you are good advised to provide them wrapped into
function_exists
orclass_exists
blocks, so they dont interfere with PHP versions providing those methods.我真的不知道 WordPress 插件是如何工作的,但除非你真的希望你的插件能够在旧的 PHP 版本上运行,否则你需要检查可用的功能并提供替代方案,这可能会导致你的代码变得混乱。
如果可以,并且想要教育您的用户,您可以简单地
version_compare( )
函数将版本与经过良好测试且功能齐全的 PHP 版本进行比较,并向最终用户发出教育性和解释性消息。I don't really know how Wordpress plugins work but except if you really want your to be able to run on old PHP version, you'll need to check for available function and provide an alternative, which can lead you code to be messy.
If you can, and want to educate your users, you can simply
version_compare()
function to compare version against a well tested and fully functionnal PHP version and throw a educationnal and explicative message to your end users.Re 1.) 可能有这样的应用,但也可能没有。您通常想要做的是密切关注手册中使用的每个函数,其中说明了需要哪些 PHP 版本。
Re 2.) 如果某个函数仅适用于 PHP 5.3 或 5.2,您通常会在用户贡献的注释或 Stack Overflow 上找到替换建议。 (不过要小心你使用的内容,UCN 中的很多代码不好 - 但在这种情况下,通常至少有一条评论这么说。
) 3.)这可能并不重要,但检查函数是否存在肯定是最安全的方法。
Re 1.) there may be such an application, but there also may be not. What you usually want to do is to keep close tabs on each function you use in the manual, which states what PHP version(s) are needed.
Re 2.) if a function is PHP 5.3 or 5.2 only, you will usually find a replacement suggestion in the User Contributed Notes or on Stack Overflow. (Be careful what you use though, a lot of the code in the UCN is bad - but in that case, there is usually at least one comment saying so.)
Re. 3.) it probably doesn't matter, but checking for whether a function exists is surely the safest way.