将类转换为扩展
我有一个 PHP 类,我想将其转换为 PHP 扩展。我检查了一些教程(tuxradar 的编写扩展,php.net 的扩展 php 和 zend 的扩展编写),有点复杂。
我找到了这篇文章 “如何编写 PHP 扩展”(编注:网站已失效),我想知道是否可以使用它可以使其从某个路径(例如 /home/website1/public_html/api/class.php
)获取 PHP 类,执行它并返回类实例。
这样,它将可以在同一服务器上托管的其他网站中使用 - 每个网站都可以简单地调用该函数,并且它将获得自己的实例。
这可能吗?
I have a PHP class I want to convert to a PHP extension. I checked some tutorials (tuxradar's writing extensions, php.net's extending php, and zend's extension writing) and it's a bit complicated.
I found the article "How to write PHP extensions" (ed note: site is defunct) and I wanted to know if it is possible to use this to make it grab a PHP class from a certain path (say /home/website1/public_html/api/class.php
), execute it and return the class instance.
This way it will be usable in other websites that are hosted on the same server – each can simply call the function and it will obtain its own instance.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我现在理解的问题是,用户有一个 PHP 类,他们想与多人共享,但不想共享源代码。
对此有很多解决方案,它们通常涉及将 PHP 代码转换为某种字节代码,并使用 PHP 扩展来运行字节代码。我从未使用过任何这些解决方案,但我知道以下内容:
我确信还有其他人。只需 Google 即可搜索 PHP 编译器,或 PHP 加速器。
The question as I understand it now is, The user has a PHP class that they would like to share with multiple people, but does not want to share the source code.
There are many solutions to this, they generally invovle turning the PHP code into some kind of byte code, and using a PHP extension to run the byte code. I've never used any of these solutions, but I'm aware of the following:
I'm sure there are others. Just Google for PHP Compiler, or PHP Accelerator.
一句话:我不相信,我认为这比这要多得多的工作。
In one sentence: I don't believe so, I think its a lot more work than that.
不,没有工具可以做到这一点。
无论如何,您想要的调用可以通过 auto_prepend_file 轻松完成。只需使该 ini 指令指向具有类定义的 PHP 文件,然后它将可供所有应用程序使用。
如果您不希望用户能够使用源代码,您可以使用几个 zend 扩展之一,这些扩展允许您预编译文件并以该形式使用它。
No, there is not tool that can do that.
Anyway, what you want call be easily accomplished with auto_prepend_file. Just make that ini directive point to a PHP file that has the class definition, and then it will be available to all the applications.
If you don't want the users to be able to use the source, you can use one the several zend extensions that allow you to pre-compile the file and use it in that form.
您可以通过编写 PHP 将底层 C 库函数扩展到 PHP 空间扩展。不过,我认为在你的情况下你不需要写一个。
You can extend underlying C library functions into PHP space by writing PHP extensions. However, i think in your case you don't need to write one.
我知道这是一个老问题(从 2012 年开始),但是答案已经改变,现在有一个工具可以做到这一点。 Jim Thunderbirds PHP 到 C 扩展工具集提供了一种方法,可以将一个文件中的简单类一直转换为复杂的多文件多级命名空间框架,并将其转换为 C 扩展,然后将其安装到 PHP 中服务器。
虽然在许多用例中不需要这样做,因为普通 PHP 代码在某些情况下也能正常工作,可以体验到显着的性能改进。信息页面显示,一个普通类(故意设计为需要很长时间)作为普通 PHP 需要 16.802139997482 秒,作为使用该工具构建的 PHP 扩展需要 3.9628620147705 秒。
作为附加优势,该工具还提供了附加功能。能够在同一扩展中组合 PHP 代码(将转换为 C)和本机 C 代码,从而产生更大的性能增强。上面使用的同一示例仅用了 0.14397192001343 秒的时间,此时大部分密集型代码已移至冒泡排序 C 代码,并且只需从 PHP 代码中调用它。
作为旁注,最终开发人员使用使用扩展的代码与将文件手动包含在正在开发的 PHP 文件中非常相似,只是不必专门包含它,因为它是通过 PHP 扩展组件完成的。
(免责声明:我不隶属于该开发人员,但很高兴遇到它,因为迄今为止它正在将我的一些强化类转换为 PHP 扩展,而无需了解 C)。
I am aware that this is an old question (being from 2012) however the answer has changed and there is now a tool that can do this. Jim Thunderbirds PHP-to-C Extension toolset provides the means to take a simple class in one file all the way up to a complicated multi file multi-level namespaced framework and convert it to a C-extension that can then be installed into your PHP server.
While in many use cases doing so is not needed as the ordinary PHP code will work just as good in some cases significant performance improvements can be experienced. The information page shows that an ordinary class (deliberately designed to take a long time) took 16.802139997482 seconds as plain vanilla PHP, and 3.9628620147705 as a PHP extension built using the tool.
As an added advantage the tool also provides an additional feature. The ability to combine PHP code (to be converted to C) and native C code within the same extension which can produce even greater performance enhancements. The same example used above only tool 0.14397192001343 seconds when much of the intensive code was moved to a bubble sort C code and simply calling it from within the PHP code.
As a side note functionally to the end developers using the code using the extension is very much similar to having the files manually included in the PHP file being developed except it doesn't have to be specifically included as it is done through the PHP extensions component.
(Disclaimer: I am not affiliated with this developer but am glad to have come across it as it is thus far working for converting some of my intensive classes into PHP extensions without needing to know C).