Convert a class to an extension

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.


    一句话:我不这么认为,我认为它的工作量要多得多。


    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.

    链接地址: http://www.djcxy.com/p/47758.html

    上一篇: functools.partial不适用于multiprocessing.Pool.map?

    下一篇: 将类转换为扩展