PHP Warning raised when Virtual Property Hooks are serialized on Models #55801
-
Laravel Version12.12.0 PHP Version8.4.3 Database Driver & VersionNo response DescriptionVirtual property hooks on Models seem to cause PHP Warnings whenever the Model is serialized. The exact warning is: There is a workaround: overriding public function __sleep()
{
$this->mergeAttributesFromCachedCasts();
$this->classCastCache = [];
$this->attributeCastCache = [];
$this->relationAutoloadCallback = null;
$this->relationAutoloadContext = null;
return array_keys(get_object_vars($this));
}to public function __sleep()
{
$this->mergeAttributesFromCachedCasts();
$this->classCastCache = [];
$this->attributeCastCache = [];
$this->relationAutoloadCallback = null;
$this->relationAutoloadContext = null;
return array_keys(get_mangled_object_vars(this));
}Steps To Reproduce
public string $prop {
get => 'prop';
}
unserialize(serialize(new User))This results in the serialization working, but it raises a PHP warning. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
|
Hi. Besides the fact that the issue is legit and the proposed solution solves it, can you please elaborate why would you use property hooks on a model? Is this related to columns from db? |
Beta Was this translation helpful? Give feedback.
-
|
framework/src/Illuminate/Queue/SerializesModels.php Lines 39 to 41 in ae955ad Model are already handled correctly for queue, so I'm not sure manual |
Beta Was this translation helpful? Give feedback.
-
|
Safe alternative for property hooks in eloquent model: |
Beta Was this translation helpful? Give feedback.
-
|
I think this is addressed in this change: #57557 |
Beta Was this translation helpful? Give feedback.
-
|
Here is possible situation when getter start hard logic. get_object_vars calling this getter every time... It might be better to replace get_object_vars with ReflectionClass ... |
Beta Was this translation helpful? Give feedback.
I think this is addressed in this change: #57557