1
- Extension skeleton
2
- ==================
1
+ A look into a PHP extension and extension skeleton
2
+ ==================================================
3
3
4
4
Here we detail what a PHP extension look like, and how to generate a skeleton using some tools. That will allow us to
5
5
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
13
13
You remember :doc: `the chapter about building PHP extensions <../build_system/building_extensions >`, so you know how
14
14
to compile/build it and install it.
15
15
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
+
16
39
When PHP starts, it quickly goes to parse its different INI files. If present, those later may declare extensions to
17
40
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.
19
48
20
49
To load extensions, `libdl <https://en.wikipedia.org/wiki/Dynamic_loading >`_ and its
21
50
`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.
36
65
`ext/standard/dl.c <https://github.com/php/php-src/blob/27d681435174433c3a9b0b8325361dfa383be0a6/ext/
37
66
standard/dl.c#L90> `_
38
67
39
- What is an extension ?
40
- **********************
68
+ What is a PHP extension ?
69
+ *************************
41
70
42
71
A PHP extension, not to be confused with a :doc: `Zend extension <zend_extensions >`, is set up by the usage of a
43
72
``zend_module_entry `` structure::
@@ -86,7 +115,8 @@ The ``ini_entry`` vector is actually unused. You :doc:`register INI entries <ini
86
115
87
116
Then you may declare dependencies, that means that your extension could need another extension to be loaded before it,
88
117
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
90
120
91
121
After that you declare a ``name ``. Nothing to say, this name is the name of your extension (which can be different from
92
122
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