Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 546
Wiika Avatar asked Sep 03 '10 23:09

Wiika


People also ask

What is the extension method for a class?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What does it mean to extend a class C#?

In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.

Can we extend String class in C#?

Yes, you can extend existing types by using extension methods. Extension methods, naturally, can only access the public interface of the type.

Can you add extension methods to an existing static class?

No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.


1 Answers

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.

like image 158
bramp Avatar answered Oct 04 '22 21:10

bramp