attributes as private variable #42750
-
I faced an issue when column in database was named attributes. Then when I wanted to change row with this column it changed this variable $attributes and created error "Cannot access offset of type string on string" in line 939. Solution: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I could not reproduce it, assigning to a non-public attribute, even when there is protected property named as the same, should trigger the magic method Maybe isn't something else is triggering the error? A trait, a third-party package? How are you assigning the value to the attributes property? You can check the execution on PHP from 7.4 to 8.1 (as of this writing) here: Test code: <?php
class Model {
protected $attributes = [];
public function __set($key, $value)
{
echo 'called magic method: ', var_export([$key => $value], true), \PHP_EOL;
}
}
$model = new \Model();
$model->attributes = 'foo';
var_dump($model); output called magic method: array (
'attributes' => 'foo',
)
object(Model)#1 (1) {
["attributes":protected]=>
array(0) {
}
} |
Beta Was this translation helpful? Give feedback.
-
Many projects use As a workaround just use |
Beta Was this translation helpful? Give feedback.
Many projects use
$this->attributes
on models, this will break everything.As a workaround just use
setAttribute
orsetRawAttributes
methods (depending on the task).