I am working on a simple ORM solution and faced a difficult situation. Ideally, I would like to use methods both in a static context and in the context of an object, depending on what it is called. I'm not sure if this is possible, but here is what I mean:
Say the user model wants to call where () statically, this currently works fine, for example:
$user = User::where('id = ?', 3);
Now I also maintain relationships, for example, the user may have messages. When this relationship is established, I simply save an empty copy of the message model in the user model and set the foreign key. For instance:
$user -> messages = new Message();
$user -> messages -> foreign_key = 'user_id';
Now, ideally, I would like to be able to call:
$user -> messages -> where('unread = ?', 1);
$this → foreign_key, , , . PHP? $this $this ( , , $this )
- ? , , static, , .