开发者

Convert a class to an extension

开发者 https://www.devze.com 2023-01-14 04:29 出处:网络
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 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?


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:

  • phc is an open source compiler for PHP
  • Zend Guard
  • HipHop for PHP - I'm unsure about this, but Facebook recently released it so it might be worth a look.

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.


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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消