# Migrating to `v0.17`
## Borrowed Access to the Rust Value of a Class
In v0.17, the Rust value of a `#[php_class]` object is in a borrow cell. A PHP call
from a method cannot get a second mutable reference to the same value. The
cycle collector does not read the value while a `&mut self` method changes it.
Refer to [Access to the Rust Value](../macros/classes.md#access-to-the-rust-value)
for the borrow rules.
### Class Parameters
`&T`, `&mut T`, `Option<&T>` and `Option<&mut T>` parameters of a class type
keep their syntax. In v0.17, the generated function borrows the Rust value for
the whole call. If two parameters, or the object of the method and a
parameter, get the same object and the borrows conflict, PHP throws an `Error`.
In v0.16, this call gave undefined behavior. v0.16 did not accept
`Option<&mut T>`, v0.17 accepts it.
The borrow guards `ClassRef<T>` and `ClassRefMut<T>` are also parameter types.
A variadic parameter of a class type must use them, for example
`&[ClassRef<T>]`. The prelude contains the two guards.
A class parameter type must implement the new trait `PhpClass`. The
`#[php_class]` macro and `class_derives!` implement it. If you implement
`RegisteredClass` by hand without `class_derives!`, add `impl PhpClass for T {}`.
### References from a Guard
You can use a reference from a `ClassRef<T>` guard only before Rust drops the
guard. Keep the guards while you use the references.
#### Before (v0.16)
```rust,ignore
let names: Vec<&str> = values
.iter()
.filter_map(|value| value.extract::<&Person>())
.map(|person| person.name.as_str())
.collect();
```
#### After (v0.17)
```rust,ignore
let people: Vec<ClassRef<Person>> = values
.iter()
.filter_map(|value| value.extract::<ClassRef<Person>>())
.collect();
### Class Objects in a `Zval`
`Zval::extract::<ClassRef<T>>()` gives `None` if the value is not a `T`. It also
gives `None` if a method of the object borrows the Rust value. If your code uses
`extract` to find the class of a value, use `Zval::try_extract`. Refer to
[Class Objects in a `Zval`](../macros/classes.md#class-objects-in-a-zval).
#### Before (v0.16)
```rust,ignore
if let Some(column) = value.extract::<&Column>() {
return Ok(column.len());
}
```
#### After (v0.17)
```rust,ignore
match value.try_extract::<ClassRef<Column>>() {
Ok(column) => return Ok(column.len()),
Err(Error::ZvalConversion(_)) => {}
Err(err) => return Err(err.into()),
}
```
### `self_` Parameters
A `self_: &mut ZendClassObject<Self>` parameter does not compile. Use
`self_: &ZendClassObject<Self>` and borrow the Rust value.
#### Before (v0.16)
```rust,ignore
pub fn rename(self_: &mut ZendClassObject<Self>, name: String) -> &mut ZendClassObject<Self> {
self_.name = name;
self_
}
```
#### After (v0.17)
```rust,ignore
pub fn rename(self_: &ZendClassObject<Self>, name: String) -> PhpResult<&ZendClassObject<Self>> {
self_.try_borrow_mut()?.name = name;
Ok(self_)
}
```
### `ZendClassObject` and Related API
| `obj.obj`, the public field | `obj.try_borrow()` and `obj.try_borrow_mut()` |
| `Deref` and `DerefMut` to `T` | `obj.try_borrow()` and `obj.try_borrow_mut()` |
| `ZendClassObject::from_zend_obj_mut` | `ZendClassObject::from_zend_obj` |
| `initialize(&mut self, T) -> Option<T>` | `initialize(&self, T) -> Result<Option<T>, BorrowError>` |
| `unsafe fn ZendClassObject::new_uninit` | `fn ZendClassObject::new_uninit` |
| `IntoZval for &mut ZendClassObject<T>` | `IntoZval for &ZendClassObject<T>` |
| `ExecuteData::parser_method` and `ExecuteData::get_object` give `Option<&mut ZendClassObject<T>>` | They give `Option<&ZendClassObject<T>>`. `get_object` takes `&self`. |
| `FromZval for &T` and `FromZvalMut for &mut T` from `class_derives!` and `#[php_interface]` | `FromZval for ClassRef<T>` and `FromZval for ClassRefMut<T>` |
| `PhpClosure::invoke<'a>(&'a mut self, parser: ArgParser<'a, '_>, ret: &mut Zval)` | `PhpClosure::invoke(&mut self, parser: ArgParser<'_, '_>, ret: &mut Zval)` |
| `Arg::try_variadic_vals` gives `Option<Vec<T>>` | It gives `Result<Vec<T>>` with the error of the first value that does not convert |
A conflict gives a `BorrowError`. PHP gets the conflict as an `Error`. The new
variant `Error::Borrow` holds a `BorrowError` that a conversion gives.
### Calls That Throw an `Error` in v0.17
In v0.16, these calls gave undefined behavior or a panic. In v0.17, PHP throws
an `Error`:
- A callback calls a `&mut self` method on an object while a different method
of the object runs.
- A callback calls a `&self` method or reads a property while a `&mut self`
method of the object runs.
- A destructor reads a property of an object while a setter of the object runs.
- A Rust closure from `Closure::wrap` calls the same closure again through PHP.
- A method runs on an object that has no Rust value, for example a subclass
that does not call `parent::__construct()`. The message is
`Object of class Foo is not initialized`.
- Two class parameters, or the object of the method and a class parameter, get
the same object and the borrows conflict.
To let a callback use the object, use the pattern of
[Call PHP Code from a Method](../macros/classes.md#call-php-code-from-a-method).
`Clone` for `ZBox<ZendClassObject<T>>` panics while a `&mut self` method of the
object runs. A Rust `Drop` that calls PHP code on `$this` sees an object without
a Rust value.
### Cloning
A class whose struct does not implement `Clone` has no clone handler, like the
uncloneable classes of PHP.
| `ReflectionClass::isCloneable()` returns `true` for all classes | It returns `false` when the struct does not implement `Clone` |
| A failed clone calls `__destruct` on an object without a Rust value | PHP does not call `__destruct` on the failed copy |
| The clone of an object without a Rust value throws `Trying to clone an uncloneable object of class Foo` | It throws `Object of class Foo is not initialized` |
`RegisteredClass::clone_fn() -> Option<fn(&Self) -> Self>` replaces
`RegisteredClass::clone_obj(&self) -> Option<Self>`. The `#[php_class]` macro
generates it. If you implement `RegisteredClass` by hand for a type that
implements `Clone`, return `Some(Self::clone)` from `clone_fn()`. An
implementation of `clone_obj` does not compile.