When we use artisan tinker, and reference an Eloquent model object, the REPL automatically prints the model's attributes, much like how it prints the public properties of any standard object we reference:
>>> (object) ['hello' => 'world']
=> {
+"hello": "world",
}
>>> App\User::first()
=> App\User {
id: 1,
name: "...",
}
What's less obvious to me is how these virtual attributes can be made to appear here, as if they were already defined as public properties of the class. I understand that much of the attribute assignment for the model is handled internally by the HasAttributes trait, but even looking there, I still don't see how the Laravel authors were able to achieve this behavior.
I've tried building a class like this:
class Bunch implements Arrayable, ArrayAccess, Jsonable, JsonSerializable { ... }
but even with working array access and a toArray method, when I reference it directly from artisan tinker:
>>> $b = new Bunch()
=> Bunch {}
>>> $b->one = 1
=> 1
>>> $b['one']
=> 1
>>> $b
=> Bunch {}
How can we change the representation that the REPL uses when it prints an object like this?