4 days ago

Pass Model by Reference?

Posted 4 days ago by bionary

I'm getting an error:

Indirect modification of overloaded property App\Task::$errors has no effect

I'm pretty sure this is one of those "pass by value" instead of "by reference" issues.

I have a model: Task $task Which I'm passing to another class

The $task contains an attribute: $task->errors;

Which I continually add error messages to as an array. I run into trouble when inside a class and doing this: public function getBrandsList(Task $task){ $task[] = 'Another error.'; }

I would have thought that the following would have rectified this but it doesn't: public function getBrandsList(Task &$task){ ...

The following works but it sure looks and feels sloppy: public function getBrandsList(Task $task){ $task->errors[] = array_merge($task->errors, ['Another error.']); }

Is there is cleaner way to do this? Is there a way to pass my Task $task model by reference?


Please sign in or create an account to participate in this conversation.