@@ -225,10 +225,100 @@ pub fn test_class(string: String, number: i32) -> TestClass {
225225 }
226226}
227227
228+ #[ php_class]
229+ pub struct MagicMethod ( i64 ) ;
230+
231+ #[ php_impl]
232+ impl MagicMethod {
233+ pub fn __construct ( ) -> Self {
234+ Self ( 0 )
235+ }
236+
237+ pub fn __destruct ( & self ) { }
238+
239+ pub fn __call ( & self , name : String , _arguments : HashMap < String , & Zval > ) -> Zval {
240+ let mut z = Zval :: new ( ) ;
241+ if name == "callMagicMethod" {
242+ let s = "Hello" . to_string ( ) ;
243+
244+ let _ = z. set_string ( s. as_str ( ) , false ) ;
245+ z
246+ } else {
247+ z. set_null ( ) ;
248+ z
249+ }
250+ }
251+
252+ pub fn __call_static ( name : String , arguments : HashMap < String , & Zval > ) -> Zval {
253+ let mut zval = Zval :: new ( ) ;
254+ if name == "callStaticSomeMagic" {
255+ let concat_args = format ! (
256+ "Hello from static call {}" ,
257+ arguments
258+ . iter( )
259+ . filter( |( _, v) | v. is_long( ) )
260+ . map( |( _, s) | s. long( ) . unwrap( ) . to_string( ) )
261+ . collect:: <Vec <_>>( )
262+ . join( " " )
263+ ) ;
264+
265+ let _ = zval. set_string ( & concat_args, false ) ;
266+ zval
267+ } else {
268+ zval. set_null ( ) ;
269+ zval
270+ }
271+ }
272+
273+ pub fn __get ( & self , name : String ) -> Zval {
274+ let mut v = Zval :: new ( ) ;
275+ v. set_null ( ) ;
276+ if name == "count" {
277+ v. set_long ( self . 0 ) ;
278+ }
279+
280+ v
281+ }
282+
283+ pub fn __set ( & mut self , prop_name : String , val : & Zval ) {
284+ if val. is_long ( ) && prop_name == "count" {
285+ self . 0 = val. long ( ) . unwrap ( )
286+ }
287+ }
288+
289+ pub fn __isset ( & self , prop_name : String ) -> bool {
290+ "count" == prop_name
291+ }
292+
293+ pub fn __unset ( & mut self , prop_name : String ) {
294+ if prop_name == "count" {
295+ self . 0 = 0 ;
296+ }
297+ }
298+
299+ pub fn __to_string ( & self ) -> String {
300+ self . 0 . to_string ( )
301+ }
302+
303+ pub fn __invoke ( & self , n : i64 ) -> i64 {
304+ self . 0 + n
305+ }
306+
307+ pub fn __debug_info ( & self ) -> HashMap < String , Zval > {
308+ let mut h: HashMap < String , Zval > = HashMap :: new ( ) ;
309+ let mut z = Zval :: new ( ) ;
310+ z. set_long ( self . 0 ) ;
311+ h. insert ( "count" . to_string ( ) , z) ;
312+
313+ h
314+ }
315+ }
316+
228317#[ php_module]
229318pub fn build_module ( module : ModuleBuilder ) -> ModuleBuilder {
230319 module
231320 . class :: < TestClass > ( )
321+ . class :: < MagicMethod > ( )
232322 . function ( wrap_function ! ( test_str) )
233323 . function ( wrap_function ! ( test_string) )
234324 . function ( wrap_function ! ( test_bool) )
@@ -321,6 +411,7 @@ mod integration {
321411 mod closure;
322412 mod globals;
323413 mod iterator;
414+ mod magic_method;
324415 mod nullable;
325416 mod number;
326417 mod object;
0 commit comments