@@ -18,7 +18,10 @@ use crate::{
1818 ZEND_PROPERTY_ISSET ,
1919 } ,
2020 errors:: { Error , Result } ,
21- php:: { class:: ClassEntry , execution_data:: ExecutionData , types:: string:: ZendString } ,
21+ php:: {
22+ class:: ClassEntry , enums:: DataType , execution_data:: ExecutionData ,
23+ types:: string:: ZendString ,
24+ } ,
2225} ;
2326
2427use super :: {
@@ -154,6 +157,11 @@ impl ZendObject {
154157 fn mut_ptr ( & self ) -> * mut Self {
155158 ( self as * const Self ) as * mut Self
156159 }
160+
161+ /// Increments the objects reference counter by 1.
162+ pub ( crate ) fn refcount_inc ( & mut self ) {
163+ self . gc . refcount += 1 ;
164+ }
157165}
158166
159167impl Debug for ZendObject {
@@ -179,7 +187,7 @@ impl Debug for ZendObject {
179187///
180188/// Implements a function `create_object` which is passed to a PHP class entry to instantiate the
181189/// object that will represent an object.
182- pub trait ZendObjectOverride {
190+ pub trait ZendObjectOverride : Default {
183191 /// Creates a new Zend object. Also allocates space for type T on which the trait is
184192 /// implemented on.
185193 ///
@@ -201,6 +209,19 @@ pub trait ZendObjectOverride {
201209 fn set_class ( ce : & ' static ClassEntry ) ;
202210}
203211
212+ impl < T : ZendObjectOverride > IntoZval for & T {
213+ const TYPE : DataType = DataType :: Object ;
214+
215+ fn set_zval ( & self , zv : & mut Zval , _: bool ) -> Result < ( ) > {
216+ unsafe {
217+ let obj = ZendClassObject :: from_obj_ptr ( * self ) . ok_or ( Error :: InvalidPointer ) ?;
218+ zv. set_object ( & mut obj. std ) ;
219+ }
220+
221+ Ok ( ( ) )
222+ }
223+ }
224+
204225/// A Zend class object which is allocated when a PHP
205226/// class object is instantiated. Overrides the default
206227/// handler when the user provides a type T of the struct
@@ -262,6 +283,20 @@ impl<T: Default> ZendClassObject<T> {
262283 ptr. as_mut ( )
263284 }
264285 }
286+
287+ /// Returns a reference to the [`ZendClassObject`] of a given object `T`.
288+ ///
289+ /// # Parameters
290+ ///
291+ /// * `obj` - The object to get the [`ZendClassObject`] for.
292+ ///
293+ /// # Safety
294+ ///
295+ /// Caller must guarantee that the given `obj` was created by Zend, which means that it
296+ /// is immediately followed by a [`zend_object`].
297+ pub ( crate ) unsafe fn from_obj_ptr ( obj : & T ) -> Option < & mut Self > {
298+ ( ( obj as * const T ) as * mut Self ) . as_mut ( )
299+ }
265300}
266301
267302impl < T : Default + Debug > Debug for ZendClassObject < T > {
0 commit comments