Skip to content

Commit 1a468b6

Browse files
Docs: add a page about zend_constant (GH-17072)
1 parent d22abca commit 1a468b6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

docs/source/core/data-structures/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
zval
99
reference-counting
1010
zend_string
11+
zend_constant
1112

1213
This section provides an overview of the core data structures used in php-src.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
###############
2+
zend_constant
3+
###############
4+
5+
PHP constants (referring to non-class constants) are stored in a dedicated structure
6+
``zend_constant``, which holds both the value of the constant and details for using it.
7+
8+
************
9+
definition
10+
************
11+
12+
.. code:: c
13+
14+
typedef struct _zend_constant {
15+
zval value;
16+
zend_string *name;
17+
zend_string *filename;
18+
} zend_constant;
19+
20+
The ``value`` field stores both the value itself and some metadata. The ``name`` and ``filename``
21+
store the name of the constant and the name of the file in which it was defined.
22+
23+
*******
24+
value
25+
*******
26+
27+
The value of the constant is stored in the :doc:`./zval` ``value``. However, since the ``zval``
28+
structure has extra space, for constants this is used to store both the number of the module that
29+
the constant was defined in, and a combination of the flags that affect the usage of the constant.
30+
31+
This extra information is placed in the ``uint32_t`` field ``value.u2.constant_flags``.
32+
33+
The bottom 16 bits are used to hold flags about the constant
34+
35+
.. code:: c
36+
37+
#define CONST_PERSISTENT (1<<0) /* Persistent */
38+
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
39+
#define CONST_DEPRECATED (1<<2) /* Deprecated */
40+
#define CONST_OWNED (1<<3) /* constant should be destroyed together
41+
with class */
42+
43+
These bottom 16 bits can be accessed with the ``ZEND_CONSTANT_FLAGS()`` macro, which is given a
44+
``zend_constant`` pointer as a parameter.
45+
46+
On the other hand, the top 16 bits are used to store the number of the PHP module that registered
47+
the constant. For constants defined by the user, the module number stored will be
48+
``PHP_USER_CONSTANT``. This module number can be accessed with the ``ZEND_CONSTANT_MODULE_NUMBER()``
49+
macro, which is likewise given a ``zend_constant`` pointer as a parameter.
50+
51+
******
52+
name
53+
******
54+
55+
The ``name`` holds a :doc:`zend_string` with the name of the constant, to allow searching for
56+
constants that have already been defined. This string is released when the constant itself is freed.
57+
58+
**********
59+
filename
60+
**********
61+
62+
Finally, the ``filename`` holds another ``zend_string`` with the name of the file in which the
63+
constant was defined, or ``NULL`` if not defined userland code. This field provides the foundation
64+
for the PHP method ``ReflectionConstant::getFileName()``.

0 commit comments

Comments
 (0)