Convert a class to an extension
Asked Answered
W

5

7

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?

Watereddown answered 3/9, 2010 at 23:49 Comment(2)
Are you trying to allow others to use your class without them being able to view the source? If not, just copy the class.php into the filesystem somewhere where the other sites can get it. Failing that just make many copies. You wouldn't typically turn your PHP class into an extension unless you were doing it for performance reasons.Reduplicative
yes i need to let others use the class without veiwing the source, thanks for replyWatereddown
R
4

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.

Reduplicative answered 4/9, 2010 at 11:1 Comment(0)
C
0

In one sentence: I don't believe so, I think its a lot more work than that.

Couchman answered 4/9, 2010 at 1:7 Comment(0)
G
0

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.

Gujral answered 4/9, 2010 at 22:47 Comment(2)
i did use auto_prepend_file but i had a problem with open_basedir restriction, is there anything i can do to bypass it ?Watereddown
@Wikka Yes, you can the path where the file is to open_basedir.Gujral
P
0

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.

Paddy answered 12/9, 2010 at 8:58 Comment(0)
I
0

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

Isosteric answered 25/1, 2017 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.