What is the difference between extension and library in PHP?

Asked

Viewed 295 times

5

In PHP there are some extensions within the language. For example, the SPL, to PHAR and the PDO are some of these extensions.

Basically, my doubts are as follows::

  • What’s the difference between an extension to a library?
  • Would it be correct or not to call them libraries because they are a set of classes?

PS: When I refer to extension, I mean language extensions and not file formats and things like.

  • Extension is that which extends a language functionality, in PHP usually (if not always) are written in C. A library is a set of structures, not necessarily classes, that help in solving a problem.

1 answer

4


What is PHAR in PHP

First I want to make it clear that PHAR files are not extensions, they are "packages" that can contain an entire application (and usually contain) to facilitate installation and distribution, that is, it groups a series of files into a single file .phar that can be executed entirely or partially using the protocol phar:// within other php scripts, example to access a specific file:

include 'phar:///caminho/para/meuarquivo.phar/olamundo.php';

This is not to say that phar cannot be treated with a library, or a framework, but anyway what will basically define this is its content and as a . Specific phar was designed to be used, ie the developer defines this.

In short, a .phar in itself does not characterize as a library, by default it is a package of anything with a series of compressed files, which may or may not run separately (via include) or as an entire application, vaguely resembling the .jar used in Java environments, which can function as function packages or even as whole applications.

What is a library

About libraries you may already have the answer here What is the difference of API, library and Framework?, but to give a clearer and very simple example in PHP, a library could be a set of functions and/or classes defined for one or more uses that usually aims to facilitate a task, ie you do not need to have a working knowledge, just need to know how to use itwere in its final application, of course this is quite vague in the way I explained, but I believe so far is enough to understand.

Extensions in PHP

In php we specifically have by common understanding which extensions are the . dll or . so (in Unix-like environments) as being binaries containing libraries or even whole frameworks that extend basic PHP functionality that usually (depends a little on how you will compile) most internal PHP Apis are extensions that are enabled or disabled in php.ini via parameters:

extension=xyz.dll

Xyz.dll is just an example

Or even the whole way:

extension=C:\foo\bar\xyz.dll

Of course, creating a file is not enough .c and compile in Mingw to be able to create an extension for PHP, a . dll has to be compiled by the same type of compiler that was compiled the PHP of your machine or server, ie it is not so simple, and outside this need to understand how to make the dll "communicate with php" that can follow here:

Now some details:

  • A php extension can be a library
  • An extension for php can be a framework
  • An extension for php may not provide any function for the programmer but can do things automatically, such as Xcache or Opcache (two extensions for PHP), which do the scripts. php on a server run faster due to a kind of "build" (bytecode, each its own way) and also helps to greatly reduce memory consumption

of course both . dll/. so cache provide some specific functions, but could not provide, which would not make them any longer extensions

As I said, an extension is written in (but there are unofficial alternative means in C++), an example of the structure of an uncompiled extension would be something like (according to the link I already mentioned above):

ext/
 └─── foobar/
       ├─── config.m4
       ├─── config.w32
       ├─── foobar_util.h
       ├─── foobar_util.c
       ├─── php_foobar.h
       ├─── foobar.c
       ├─── package.xml
       ├─── CREDITS
       └─── tests/
             ├─── critical_function_001.phpt
             ├─── critical_function_002.phpt
             ├─── optional_function_001.phpt
             └─── optional_function_002.phpt

The briefcase tests are just tests you write similar to unit tests.

  • config.m4 is the build system configuration for UNIX-like (see link for more details https://www.php.net/manual/en/internals2.buildsys.configunix.php)

  • config.w32 is the build system configuration for Windows (see the link for more details https://www.php.net/manual/en/internals2.buildsys.configwin.php)

  • php_foobar.h

    When building an extension as a static module in "PHP binary", the build system expects a header file with php_ as prefix to the extension name, which includes a statement for a pointer to the extension module structure. This file usually contains additional macros, prototypes and global, as well as any header.

  • foobar.c

    Main source file of the extension. By convention, the name of this file is the name of the extension, but this is not a requirement. This file contains the module structure declaration, entries INI, management functions, user space functions and other requirements of an extension.

The other files .h in the example are just "separations" that you can or cannot do and then use with #include.

Term extension

Now just about the term extension, even in PHP, or just talking about it, extension could easily be a term used for different situations, apart from the extensions already mentioned before, this I refer to the language level and not the level of "PHP interpreter" (the executable php.exe in windows for example).

For example, you have a php application that is a website, so you create a PHP script that adds some specific routine to your site, would call extension, is the most appropriate term? I don’t think so, especially since it would cause confusion.

Now about PHP classes, when you create something like this:

class Foo {}

class Bar extends Foo {}

The class Bar extends (or not) class methods and/or properties Foo, then possibly Bar will be an extension to Foo, but of course calling it an extension will confuse a little, I think it’s easier to say clearly what it is, Bar is a "extended class"


Difference between PHAR and an extension (.dll/. so)

Phar are packages and can be executed directly, even via the command line, already as . dlls are executed from the moment a PHP server is started or at the moment that PHP is run (startup), that is to say it is as if PHAR is the PHP part and already the dlls are running all the time, of course both may contain similar things, but basically perform at different times.


Replying to the questionnaire

What’s the difference between an extension to a library?

I believe I have answered above, but briefly PHP extensions can have libraries and/or functions or have nothing but an automatic routine (such as a system to generate bytecode PHP scripts to improve the performance of PHP applications) and the library can go into this, or not.

Would it be correct or not to call them libraries because they are a set of classes?

PHAR or extensions are not libraries, but may be, depending on what you have inside, may also not contain anything at all and will still be an extensions and files .phar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.