Enum ext_php_rs::props::Property
source · pub enum Property<'a, T> {
Field(Box<dyn Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync>),
Method {
get: PropertyGetter<'a, T>,
set: PropertySetter<'a, T>,
},
}
Expand description
Represents a property added to a PHP class.
There are two types of properties:
- Field properties, where the data is stored inside a struct field.
- Method properties, where getter and/or setter functions are provided, which are used to get and set the value of the property.
Variants§
Implementations§
source§impl<'a, T: 'a> Property<'a, T>
impl<'a, T: 'a> Property<'a, T>
sourcepub fn method<V>(
get: Option<fn(_: &T) -> V>,
set: Option<fn(_: &mut T, _: V)>
) -> Self
pub fn method<V>( get: Option<fn(_: &T) -> V>, set: Option<fn(_: &mut T, _: V)> ) -> Self
Creates a method property with getters and setters.
If either the getter or setter is not given, an exception will be thrown when attempting to retrieve/set the property.
§Parameters
get
- Function used to get the value of the property, in anOption
.set
- Function used to set the value of the property, in anOption
.
§Examples
struct Test;
impl Test {
pub fn get_prop(&self) -> String {
"Hello".into()
}
pub fn set_prop(&mut self, val: String) {
println!("{}", val);
}
}
let prop: Property<Test> = Property::method(Some(Test::get_prop), Some(Test::set_prop));
sourcepub fn get(&self, self_: &'a mut T, retval: &mut Zval) -> PhpResult
pub fn get(&self, self_: &'a mut T, retval: &mut Zval) -> PhpResult
Attempts to retrieve the value of the property from the given object
self_
.
The value of the property, if successfully retrieved, is loaded into the
given Zval
retval
. If unsuccessful, a PhpException
is
returned inside the error variant of a result.
§Parameters
self_
- The object to retrieve the property from.retval
- TheZval
to load the value of the property into.
§Returns
Nothing upon success, a PhpException
inside an error variant when
the property could not be retrieved.
§Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|obj: &mut Test| &mut obj.a);
let mut test = Test { a: 500 };
let mut zv = Zval::new();
prop.get(&mut test, &mut zv).unwrap();
assert_eq!(zv.long(), Some(500));
sourcepub fn set(&self, self_: &'a mut T, value: &Zval) -> PhpResult
pub fn set(&self, self_: &'a mut T, value: &Zval) -> PhpResult
Attempts to set the value of the property inside the given object
self_
.
The new value of the property is supplied inside the given Zval
value
. If unsuccessful, a PhpException
is returned inside the
error variant of a result.
§Parameters
self_
- The object to set the property in.value
- TheZval
containing the new content for the property.
§Returns
Nothing upon success, a PhpException
inside an error variant when
the property could not be set.
§Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|obj: &mut Test| &mut obj.a);
let mut test = Test { a: 500 };
let zv = 100.into_zval(false).unwrap();
prop.set(&mut test, &zv).unwrap();
assert_eq!(test.a, 100);