PHP extension vs. library (and can it be converted)
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.
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.
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.
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).
精彩评论