pub struct ClassBuilder { /* private fields */ }
Expand description

Builder for registering a class in PHP.

Implementations§

Creates a new class builder, used to build classes to be exported to PHP.

Parameters
  • name - The name of the class.
Examples found in repository?
src/closure.rs (line 125)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    pub fn build() {
        if CLOSURE_META.has_ce() {
            panic!("Closure has already been built.");
        }

        let ce = ClassBuilder::new("RustClosure")
            .method(
                FunctionBuilder::new("__invoke", Self::invoke)
                    .not_required()
                    .arg(Arg::new("args", DataType::Mixed).is_variadic())
                    .returns(DataType::Mixed, false, true)
                    .build()
                    .expect("Failed to build `RustClosure` PHP class."),
                MethodFlags::Public,
            )
            .object_override::<Self>()
            .build()
            .expect("Failed to build `RustClosure` PHP class.");
        CLOSURE_META.set_ce(ce);
    }

Sets the class builder to extend another class.

Parameters
  • parent - The parent class to extend.

Implements an interface on the class.

Parameters
  • interface - Interface to implement on the class.
Panics

Panics when the given class entry interface is not an interface.

Adds a method to the class.

Parameters
  • func - The function entry to add to the class.
  • flags - Flags relating to the function. See MethodFlags.
Examples found in repository?
src/closure.rs (lines 126-134)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    pub fn build() {
        if CLOSURE_META.has_ce() {
            panic!("Closure has already been built.");
        }

        let ce = ClassBuilder::new("RustClosure")
            .method(
                FunctionBuilder::new("__invoke", Self::invoke)
                    .not_required()
                    .arg(Arg::new("args", DataType::Mixed).is_variadic())
                    .returns(DataType::Mixed, false, true)
                    .build()
                    .expect("Failed to build `RustClosure` PHP class."),
                MethodFlags::Public,
            )
            .object_override::<Self>()
            .build()
            .expect("Failed to build `RustClosure` PHP class.");
        CLOSURE_META.set_ce(ce);
    }
More examples
Hide additional examples
src/builders/class.rs (lines 211-220)
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    pub fn object_override<T: RegisteredClass>(mut self) -> Self {
        extern "C" fn create_object<T: RegisteredClass>(_: *mut ClassEntry) -> *mut ZendObject {
            // SAFETY: After calling this function, PHP will always call the constructor
            // defined below, which assumes that the object is uninitialized.
            let obj = unsafe { ZendClassObject::<T>::new_uninit() };
            obj.into_raw().get_mut_zend_obj()
        }

        zend_fastcall! {
            extern fn constructor<T: RegisteredClass>(ex: &mut ExecuteData, _: &mut Zval) {
                let ConstructorMeta { constructor, .. } = match T::CONSTRUCTOR {
                    Some(c) => c,
                    None => {
                        PhpException::default("You cannot instantiate this class from PHP.".into())
                            .throw()
                            .expect("Failed to throw exception when constructing class");
                        return;
                    }
                };

                let this = match constructor(ex) {
                    ConstructorResult::Ok(this) => this,
                    ConstructorResult::Exception(e) => {
                        e.throw()
                            .expect("Failed to throw exception while constructing class");
                        return;
                    }
                    ConstructorResult::ArgError => return,
                };
                let this_obj = match ex.get_object::<T>() {
                    Some(obj) => obj,
                    None => {
                        PhpException::default("Failed to retrieve reference to `this` object.".into())
                            .throw()
                            .expect("Failed to throw exception while constructing class");
                        return;
                    }
                };
                this_obj.initialize(this);
            }
        }

        debug_assert_eq!(
            self.name.as_str(),
            T::CLASS_NAME,
            "Class name in builder does not match class name in `impl RegisteredClass`."
        );
        self.object_override = Some(create_object::<T>);
        self.method(
            {
                let mut func = FunctionBuilder::new("__construct", constructor::<T>);
                if let Some(ConstructorMeta { build_fn, .. }) = T::CONSTRUCTOR {
                    func = build_fn(func);
                }
                func.build().expect("Failed to build constructor function")
            },
            MethodFlags::Public,
        )
    }

Adds a property to the class. The initial type of the property is given by the type of the given default. Note that the user can change the type.

Parameters
  • name - The name of the property to add to the class.
  • default - The default value of the property.
  • flags - Flags relating to the property. See PropertyFlags.
Panics

Function will panic if the given default cannot be converted into a Zval.

Adds a constant to the class. The type of the constant is defined by the type of the given default.

Returns a result containing the class builder if the constant was successfully added.

Parameters
  • name - The name of the constant to add to the class.
  • value - The value of the constant.

Sets the flags for the class.

Parameters
  • flags - Flags relating to the class. See ClassFlags.

Overrides the creation of the Zend object which will represent an instance of this class.

Parameters
  • T - The type which will override the Zend object. Must implement RegisteredClass which can be derived using the php_class attribute macro.
Panics

Panics if the class name associated with T is not the same as the class name specified when creating the builder.

Examples found in repository?
src/closure.rs (line 135)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    pub fn build() {
        if CLOSURE_META.has_ce() {
            panic!("Closure has already been built.");
        }

        let ce = ClassBuilder::new("RustClosure")
            .method(
                FunctionBuilder::new("__invoke", Self::invoke)
                    .not_required()
                    .arg(Arg::new("args", DataType::Mixed).is_variadic())
                    .returns(DataType::Mixed, false, true)
                    .build()
                    .expect("Failed to build `RustClosure` PHP class."),
                MethodFlags::Public,
            )
            .object_override::<Self>()
            .build()
            .expect("Failed to build `RustClosure` PHP class.");
        CLOSURE_META.set_ce(ce);
    }

Builds the class, returning a reference to the class entry.

Errors

Returns an Error variant if the class could not be registered.

Examples found in repository?
src/closure.rs (line 136)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    pub fn build() {
        if CLOSURE_META.has_ce() {
            panic!("Closure has already been built.");
        }

        let ce = ClassBuilder::new("RustClosure")
            .method(
                FunctionBuilder::new("__invoke", Self::invoke)
                    .not_required()
                    .arg(Arg::new("args", DataType::Mixed).is_variadic())
                    .returns(DataType::Mixed, false, true)
                    .build()
                    .expect("Failed to build `RustClosure` PHP class."),
                MethodFlags::Public,
            )
            .object_override::<Self>()
            .build()
            .expect("Failed to build `RustClosure` PHP class.");
        CLOSURE_META.set_ce(ce);
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.