8

Some php wamp/lamp packages come with php extensions packaged within like php_amf, php_db, php_gd2 and I just have to activate the extension, or install the extension if it doesn't come by default.

My question in general is, how are these extensions different from libraries? and in specific I want to know, can an extension be turned into a library that's loaded in the project itself? the goal is to call the library without special installations like php extensions need. Sometimes when you're on shared hosting, you don't have enough privileges to install a new extension.

sami
  • 7,515
  • 8
  • 33
  • 37
  • possible duplicate of http://stackoverflow.com/questions/1567605/php-extension-vs-library-vs-class-when-and-why – Shakti Singh Feb 10 '11 at 17:24
  • 1
    @Shakti Thanks for the link, but the other question doesn't specifically address the issue of conversion. This is what I'm mostly interested in. – sami Feb 10 '11 at 17:31

3 Answers3

13

A PHP extension is a C or C++ program, wrapped around the Zend Engine, that provides PHP functions and classes within a PHP installation.

A PHP library is a program coded in native PHP that may or may not use extensions to provide functions and classes within a PHP program.

While it is possible and fairly easy (assuming you have enough C++ knowledge) to transform a PHP library to an extension, the opposite can be a tedious process, because the C++ program may use functions and objects that are not available in PHP.

It's easier to convert a PHP library to an extension, because obviously the PHP functions are all available in C, one way or another, since PHP is coded in C. The opposite is not always true however.

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • +1 but I'm wondering more about the issue of converting an extension to a library. Do you have specific comments on that? – sami Feb 10 '11 at 17:26
  • But I thought that PHP is a turing-complete language, which means that anything that's been done in C (or any language), PHP can also do – sami Feb 10 '11 at 17:46
  • @sami: I didn't say that PHP cannot do it, I said it was tedious. Per example, in C++ you have a notion of friendship between classes, multiple inheritance, constant arguments and return values, none of which PHP currently have. It is possible to simulate such things, but it is not as easy as renaming a function or changing a few arguments. Some of these changes requires one to change the program design. On the other hand, the C++ language supports almost all PHP language features. – netcoder Feb 10 '11 at 18:16
4

A PHP extension is written in another language (usually C or C++) and extends PHP to allow it to do something that couldn't be done in practice with PHP. For example, direct interaction with the operating system or web server that isn't already supported by a PHP built-in function. Extensions also allow existing code written in other languages to be reused from PHP; even though the library could in theory be rewritten in PHP, it will quite often be impractical to do so, and reusing code gives more features with less effort and allows future updates to the code to be incorporated with little or no effort.

A PHP library is just a shared collection of PHP code, so although it allows code to be reused by more than one developer, it doesn't enable you to do anything that you couldn't (theoretically) write PHP code for yourself.

In terms of converting an extension to a library: It depends what the extension does. If it can be done with raw PHP, then you can convert it, but it's pretty much a full rewrite of the functionality. It may also make the code slower.

Tim Martin
  • 3,618
  • 6
  • 32
  • 43
  • Would the person who down-voted me care to explain where I was wrong? – Tim Martin Feb 10 '11 at 17:27
  • @Tim: I did not downvote, but my guess is that the comment of "being able to do something which couldn't be done with PHP" is false -- anything an extension can do PHP can do. It's just that as an extension things will perform **significantly** better. (PHP exposes pretty much every POSIX system call already except pthreads, and you'd better not be using threads in a PHP script anyway. – Billy ONeal Feb 10 '11 at 17:29
  • 1
    Conceivably they didn't like the fact that I duplicated the answer from netcoder, but the two answers were posted within 30 seconds of each other. If I'm 90% through an answer and another one comes in, I usually just finish what I'm typing rather than abandoning. – Tim Martin Feb 10 '11 at 17:30
  • How can PHP, without help from an extension, do raw memory access? Call C/C++ functions for which a wrapper doesn't exist in the PHP standard library? Instantiate C++ classes? – Tim Martin Feb 10 '11 at 17:32
  • @Billy ONeal So this means that an extension CAN be converted to a library? – sami Feb 10 '11 at 17:33
  • @Tim: No, it cannot do raw memory access, nor can it instantiate C++ classes. But it doesn't make sense to do that in PHP; it has no memory model; and you could simply create the functionality of the C++ classes in PHP classes instead. – Billy ONeal Feb 10 '11 at 17:35
  • @Tim: What I mean is: **impossible** is a strong word. You can duplicate anything a usermode C or C++ library can do in PHP (kernel mode is another issue; PHP doesn't have means to talk with assembler or CPU instructions). It's just not very practical given that there's a lot of functionality out there already written in C or C++, and the natural way to expose that functionality is through extensions. – Billy ONeal Feb 10 '11 at 17:38
  • @Billy: OK, PHP is Turing-complete, so there's no functionality that can't be implemented in PHP if you're prepared to rewrite it. But if you have a large library in another language and rewriting it isn't an option (either it's too large, or you want 100% compatibility) then in practice it can't be done with pure PHP. A lot (the majority?) of extensions cover this sort of case. – Tim Martin Feb 10 '11 at 17:39
  • @Tim: I would agree that it would be *impractical*, but not *impossible*. There's a world of difference there. – Billy ONeal Feb 10 '11 at 17:40
  • @Billy Thanks for the feedback, I've clarified the issue in my answer. – Tim Martin Feb 10 '11 at 17:43
  • @Tim, funny how you mention Turing-complete, I just finished writing a comment about that. – sami Feb 10 '11 at 17:48
2

can an extension be turned into a library

Not automatically, no. An extension is not written in PHP; therefore it cannot be simply converted. It's of course possible to write PHP which performs the equivalent operations, but such a script would be significantly slower than the original extension because PHP is relatively inefficient for calculation (when compared with native/compiled languages).

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552