Skip to content

Commit 2cf7c66

Browse files
committed
Allow to initialise objects
1 parent 45f71b3 commit 2cf7c66

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

src/zend/handlers.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl ZendObjectHandlers {
8585
let prop_name = member
8686
.as_ref()
8787
.ok_or("Invalid property name pointer given")?;
88-
let self_ = &mut **obj;
88+
let self_ = &mut *obj;
8989
let props = T::get_metadata().get_properties();
9090
let prop = props.get(prop_name.as_str()?);
9191

@@ -132,7 +132,8 @@ impl ZendObjectHandlers {
132132
let prop_name = member
133133
.as_ref()
134134
.ok_or("Invalid property name pointer given")?;
135-
let self_ = &mut **obj;
135+
136+
let self_ = &mut *obj;
136137
let props = T::get_metadata().get_properties();
137138
let prop = props.get(prop_name.as_str()?);
138139
let value_mut = value.as_mut().ok_or("Invalid return zval given")?;

tests/src/integration/_utils.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,18 @@ function assert_exception_thrown(callable $callback): void
77
} catch (\Throwable $th) {
88
return;
99
}
10-
throw new Exception("Excption was not thrown", 255);
10+
throw new Exception("Exception was not thrown", 255);
11+
}
12+
13+
14+
function assert_specific_exception_thrown(callable $callback, string $expectedExpectionFqcn): void
15+
{
16+
try {
17+
call_user_func($callback);
18+
} catch (\Throwable $e) {
19+
if ($e instanceof $expectedExpectionFqcn) {
20+
return;
21+
}
22+
}
23+
throw new Exception("Exception was not thrown", 255);
1124
}

tests/src/integration/exception.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require('_utils.php');
4+
5+
assert_specific_exception_thrown(fn() => throw throw_default_exception(), \Exception::class);
6+
7+
assert_specific_exception_thrown(fn() => throw throw_custom_exception(), \Test\TestException::class);
8+
9+
try {
10+
throw throw_custom_exception();
11+
} catch (\Throwable $e) {
12+
// Check if object is initiated
13+
assert("Not good custom!" === $e->getMessage());
14+
}

tests/src/integration/exception.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn exception_works() {
3+
assert!(crate::integration::run_php("exception.php"));
4+
}

tests/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![cfg_attr(windows, feature(abi_vectorcall))]
2-
use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval};
2+
use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval, exception::PhpException, zend::ce};
33
use std::collections::HashMap;
44

55
#[php_function]
@@ -112,6 +112,21 @@ pub fn test_class(string: String, number: i32) -> TestClass {
112112
}
113113
}
114114

115+
#[php_class(name = "Test\\TestException")]
116+
#[extends(ce::exception())]
117+
#[derive(Debug)]
118+
pub struct TestException;
119+
120+
#[php_function]
121+
pub fn throw_custom_exception() -> PhpResult<i32> {
122+
Err(PhpException::from_class::<TestException>("Not good custom!".into()))
123+
}
124+
125+
#[php_function]
126+
pub fn throw_default_exception() -> PhpResult<i32> {
127+
Err(PhpException::default("Not good!".into()))
128+
}
129+
115130
#[php_module]
116131
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
117132
module
@@ -179,6 +194,7 @@ mod integration {
179194
mod callable;
180195
mod class;
181196
mod closure;
197+
mod exception;
182198
mod nullable;
183199
mod number;
184200
mod object;

0 commit comments

Comments
 (0)