Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 958
sami Avatar asked Feb 10 '11 17:02

sami


People also ask

What are PHP extensions used for?

php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.

What is PHP extension directory?

extension_dir = <PATH TO EXTENSIONS>: The extension_dir points to the directory where the PHP extensions are stored. The path can be fully qualified (for example, C:\PHP\ext ) or relative (for example, . \ext). Extensions that are specified lower in the Php. ini file are located in the extension_dir.


3 Answers

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.

like image 193
netcoder Avatar answered Nov 11 '22 04:11

netcoder


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.

like image 28
Tim Martin Avatar answered Nov 11 '22 05:11

Tim Martin


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

like image 30
Billy ONeal Avatar answered Nov 11 '22 06:11

Billy ONeal