• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

extphprs / ext-php-rs / 19768043942

28 Nov 2025 03:29PM UTC coverage: 35.332% (-0.03%) from 35.363%
19768043942

Pull #592

github

ptondereau
feat(php): Add PHP 8.5 support
Pull Request #592: feat(php): Add PHP 8.5 support

1 of 22 new or added lines in 2 files covered. (4.55%)

1 existing line in 1 file now uncovered.

1603 of 4537 relevant lines covered (35.33%)

8.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/constant.rs
1
//! Types and traits for registering constants in PHP.
2

3
use std::ffi::CString;
4
use std::fmt::Debug;
5

6
use super::flags::GlobalConstantFlags;
7
use crate::error::Result;
8
use crate::ffi::{
9
    zend_register_bool_constant, zend_register_double_constant, zend_register_long_constant,
10
    zend_register_string_constant,
11
};
12

13
/// Implemented on types which can be registered as a constant in PHP.
14
pub trait IntoConst: Debug {
15
    /// Registers a global module constant in PHP, with the value as the content
16
    /// of self. This function _must_ be called in the module startup
17
    /// function, which is called after the module is initialized. The
18
    /// second parameter of the startup function will be the module number.
19
    /// By default, the case-insensitive and persistent flags are set when
20
    /// registering the constant.
21
    ///
22
    /// Returns a result containing nothing if the constant was successfully
23
    /// registered.
24
    ///
25
    /// # Parameters
26
    ///
27
    /// * `name` - The name of the constant.
28
    /// * `module_number` - The module number that we are registering the
29
    ///   constant under.
30
    ///
31
    /// # Errors
32
    ///
33
    /// Returns an error if the constant could not be registered.
34
    ///
35
    /// # Examples
36
    ///
37
    /// ```no_run
38
    /// use ext_php_rs::constant::IntoConst;
39
    ///
40
    /// pub extern "C" fn startup_function(_type: i32, module_number: i32) -> i32 {
41
    ///     5.register_constant("MY_CONST_NAME", module_number); // MY_CONST_NAME == 5
42
    ///     "Hello, world!".register_constant("STRING_CONSTANT", module_number); // STRING_CONSTANT == "Hello, world!"
43
    ///     0
44
    /// }
45
    /// ```
46
    fn register_constant(&self, name: &str, module_number: i32) -> Result<()> {
×
47
        self.register_constant_flags(name, module_number, GlobalConstantFlags::Persistent)
×
48
    }
49

50
    /// Registers a global module constant in PHP, with the value as the content
51
    /// of self. This function _must_ be called in the module startup
52
    /// function, which is called after the module is initialized. The
53
    /// second parameter of the startup function will be the module number.
54
    /// This function allows you to pass any extra flags in if you require.
55
    /// Note that the case-sensitive and persistent flags *are not* set when you
56
    /// use this function, you must set these yourself.
57
    ///
58
    /// Returns a result containing nothing if the constant was successfully
59
    /// registered.
60
    ///
61
    /// # Parameters
62
    ///
63
    /// * `name` - The name of the constant.
64
    /// * `module_number` - The module number that we are registering the
65
    ///   constant under.
66
    /// * `flags` - Flags to register the constant with.
67
    ///
68
    /// # Errors
69
    ///
70
    /// Returns an error if the constant flags could not be registered.
71
    ///
72
    /// # Examples
73
    ///
74
    /// ```no_run
75
    /// use ext_php_rs::{constant::IntoConst, flags::GlobalConstantFlags};
76
    ///
77
    /// pub extern "C" fn startup_function(_type: i32, module_number: i32) -> i32 {
78
    ///     42.register_constant_flags("MY_CONST_NAME", module_number, GlobalConstantFlags::Persistent | GlobalConstantFlags::Deprecated);
79
    ///     0
80
    /// }
81
    /// ```
82
    fn register_constant_flags(
83
        &self,
84
        name: &str,
85
        module_number: i32,
86
        flags: GlobalConstantFlags,
87
    ) -> Result<()>;
88
}
89

90
impl IntoConst for String {
91
    fn register_constant_flags(
×
92
        &self,
93
        name: &str,
94
        module_number: i32,
95
        flags: GlobalConstantFlags,
96
    ) -> Result<()> {
97
        self.as_str()
×
98
            .register_constant_flags(name, module_number, flags)
×
99
    }
100
}
101

102
impl IntoConst for &str {
103
    fn register_constant_flags(
×
104
        &self,
105
        name: &str,
106
        module_number: i32,
107
        flags: GlobalConstantFlags,
108
    ) -> Result<()> {
109
        unsafe {
110
            #[cfg(php85)]
111
            {
112
                let _ = zend_register_string_constant(
113
                    CString::new(name)?.as_ptr(),
114
                    name.len() as _,
115
                    CString::new(*self)?.as_ptr(),
116
                    flags.bits().try_into()?,
117
                    module_number,
118
                );
119
            }
120
            #[cfg(not(php85))]
121
            {
122
                zend_register_string_constant(
NEW
123
                    CString::new(name)?.as_ptr(),
×
NEW
124
                    name.len() as _,
×
NEW
125
                    CString::new(*self)?.as_ptr(),
×
NEW
126
                    flags.bits().try_into()?,
×
NEW
127
                    module_number,
×
128
                );
129
            }
130
        };
131
        Ok(())
×
132
    }
133
}
134

135
impl IntoConst for bool {
136
    fn register_constant_flags(
×
137
        &self,
138
        name: &str,
139
        module_number: i32,
140
        flags: GlobalConstantFlags,
141
    ) -> Result<()> {
142
        unsafe {
143
            #[cfg(php85)]
144
            {
145
                let _ = zend_register_bool_constant(
146
                    CString::new(name)?.as_ptr(),
147
                    name.len() as _,
148
                    *self,
149
                    flags.bits().try_into()?,
150
                    module_number,
151
                );
152
            }
153
            #[cfg(not(php85))]
154
            {
155
                zend_register_bool_constant(
NEW
156
                    CString::new(name)?.as_ptr(),
×
NEW
157
                    name.len() as _,
×
NEW
158
                    *self,
×
NEW
159
                    flags.bits().try_into()?,
×
NEW
160
                    module_number,
×
161
                );
162
            }
163
        };
164
        Ok(())
×
165
    }
166
}
167

168
/// Implements the `IntoConst` trait for a given number type using a given
169
/// function.
170
macro_rules! into_const_num {
171
    ($type: ty, $fn: expr) => {
172
        impl IntoConst for $type {
173
            fn register_constant_flags(
×
174
                &self,
×
175
                name: &str,
×
176
                module_number: i32,
×
177
                flags: GlobalConstantFlags,
×
178
            ) -> Result<()> {
×
179
                unsafe {
180
                    #[cfg(php85)]
181
                    {
182
                        let _ = $fn(
183
                            CString::new(name)?.as_ptr(),
184
                            name.len() as _,
185
                            (*self).into(),
186
                            flags.bits().try_into()?,
187
                            module_number,
188
                        );
189
                    }
190
                    #[cfg(not(php85))]
191
                    {
192
                        $fn(
NEW
193
                            CString::new(name)?.as_ptr(),
×
NEW
194
                            name.len() as _,
×
NEW
195
                            (*self).into(),
×
NEW
196
                            flags.bits().try_into()?,
×
NEW
197
                            module_number,
×
198
                        );
199
                    }
200
                };
NEW
201
                Ok(())
×
202
            }
203
        }
204
    };
205
}
206

207
into_const_num!(i8, zend_register_long_constant);
208
into_const_num!(i16, zend_register_long_constant);
209
into_const_num!(i32, zend_register_long_constant);
210
into_const_num!(i64, zend_register_long_constant);
211
into_const_num!(f32, zend_register_double_constant);
212
into_const_num!(f64, zend_register_double_constant);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc