Skip to content

Commit 3592382

Browse files
author
Julien Pauli
committed
Note on ext dependencies and load order
1 parent b73ae37 commit 3592382

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

Book/php7/extensions_design/extension_skeleton.rst

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Extension skeleton
2-
==================
1+
A look into a PHP extension and extension skeleton
2+
==================================================
33

44
Here we detail what a PHP extension look like, and how to generate a skeleton using some tools. That will allow us to
55
use a skeleton code and hack into it, instead of creating every needed piece by hand from scratch.
@@ -13,9 +13,38 @@ How the engine loads extensions
1313
You remember :doc:`the chapter about building PHP extensions <../build_system/building_extensions>`, so you know how
1414
to compile/build it and install it.
1515

16+
You may build **statically compiled** extensions, those are extensions that are part of PHP's heart and melt into it.
17+
They are not represented as *.so* files, but as *.o* objects that are linked into the final PHP executable (ELF). Thus,
18+
such extensions cannot be disabled, they are part of the PHP executable body code : they are here, in, whatever you say
19+
and do. Some extensions are required to be statically built, f.e *ext/core*, *ext/standard*, *ext/spl* and
20+
*ext/mysqlnd* (non-exhaustive list).
21+
22+
You can find the list of statically compiled extensions by looking at the ``main/internal_functions.c`` that is
23+
generated while you compile PHP. This step is detailed in
24+
:doc:`the chapter about building PHP <../build_system/building_php>`.
25+
26+
Then, you may also build **dynamically loaded** extensions. Those are the famous *extension.so* files that are born at
27+
the end of the individual compilation process. Dynamically loaded extensions offer the advantage to be pluggable and
28+
unpluggable at runtime, and don't require a recompilation of all PHP to be enabled or disabled. The drawback is that
29+
the PHP process startup time is longer when it must load .so files. But that's a matter of milliseconds and you don't
30+
really suffer from that.
31+
32+
Another drawback of dynamically loaded extensions is the extension loading order. Some extensions may require other
33+
ones to be loaded before them. Although this is not a good practice, we'll see that PHP extension system allows you to
34+
declare dependencies to master such an order, but dependencies are usually a bad idea and should be avoided.
35+
36+
Last thing : PHP statically compiled extensions start before dynamically compiled ones. That means that their
37+
``MINIT()`` is called before extensions.so files' ``MINIT()``.
38+
1639
When PHP starts, it quickly goes to parse its different INI files. If present, those later may declare extensions to
1740
load using the *"extension=some_ext.so"* line reference.
18-
PHP then collects every extension parsed from INI configuration, and will try to load them.
41+
PHP then collects every extension parsed from INI configuration, and will try to load them in the same order they've
42+
been added to the INI file, until some extensions declared some dependencies (which will then be loaded before).
43+
44+
.. note:: If you use an operating system package manager, you may have noticed that packagers usually name their
45+
extension file with heading numbers, aka *00_ext.ini*, *01_ext.ini* etc... This is to master the order
46+
extensions will be loaded. Some uncommon extensions require a specific order to be run. We'd like to remind
47+
you that depending on other extensions to be loaded before yours is a bad idea.
1948

2049
To load extensions, `libdl <https://en.wikipedia.org/wiki/Dynamic_loading>`_ and its
2150
`dlopen()/dlsym() <http://www.unix.com/man-page/All/3lib/libdl/>`_ functions are used.
@@ -36,8 +65,8 @@ engine once wanting to load your extension.
3665
`ext/standard/dl.c <https://github.com/php/php-src/blob/27d681435174433c3a9b0b8325361dfa383be0a6/ext/
3766
standard/dl.c#L90>`_
3867

39-
What is an extension ?
40-
**********************
68+
What is a PHP extension ?
69+
*************************
4170

4271
A PHP extension, not to be confused with a :doc:`Zend extension <zend_extensions>`, is set up by the usage of a
4372
``zend_module_entry`` structure::
@@ -86,7 +115,8 @@ The ``ini_entry`` vector is actually unused. You :doc:`register INI entries <ini
86115

87116
Then you may declare dependencies, that means that your extension could need another extension to be loaded before it,
88117
or could declare a conflict with another extensions. This is done using the ``deps`` field. In reality, this is very
89-
ucommonly used, and more generally it is a bad pactice to create dependencies accross PHP extensions.
118+
ucommonly used, and more generally it is a bad pactice to create dependencies accross PHP extensions. Also, note that
119+
dependencies are
90120

91121
After that you declare a ``name``. Nothing to say, this name is the name of your extension (which can be different from
92122
the name of its own *.so* file). Take care the name is case sensitive in most operations, we suggest you use something

0 commit comments

Comments
 (0)