-1
abstract class BaseModel {
public function find($params){
$table = $this->$table;
//...
}
}
class User extends BaseModel {
protected $table = "users";
}
if I do:
$u = new User();
$u->find([]);
works nomally because I created a new instance of the user class. but if I want to do it directly:
User::find([]);
i do not know why no reference to the $this
how do I fix this?
I don’t understand what the problem is to be solved. Just don’t do what you can’t, or stop using OOP just because it’s trendy. Turns out what you’re trying to do is use OOP where you don’t need it, so you’re creating a problem. But without more information I can’t guarantee.
– Maniero
Easier for you to explain what result you want to get, than just to show the way you’re trying to do it. If you can, click [Edit] and explain better what behavior the code should have. Take advantage and review that
$protected
there, that is a little "weird".– Bacco
what I want is to be able to use the find() function as Static, but I also want to be able to instantiate the class and use the find() function normally.
– Henrique.Araujo
Repeating what you already said in the question does not add anything. Why the
find
would need the$this
? This and other basic information has to be in the body of the question, otherwise you don’t know what you’re trying to do, and you don’t know if the rest of the code is right. I suggest first of all to read [Ask], and then [Dit] the question with the relevant information.– Bacco
To make it clearer is something like the Standard, like: in the Standard I can do App User::find(1); without having to instantiate an App User class, in the same way I can first instantiate an App User obj and then use find ( $user->find(1) )
– Henrique.Araujo
I get it, one has to mess with Latin to understand what you’re saying. Surely if
App\User::find(1)
works in the Laravel, the answer is very simple: the Laravel does not use the$this
no find. In fact, it wouldn’t even make sense. If you need$this
in a static method, you already have error in other parts of the code.– Bacco
More and more confirms what I said initially is trying to take something that is not OOP and try to turn OOP by force. It won’t even work.
– Maniero
already solved my problem, the tableless guys knew me understand and helped me in 2 min. without complication. obg by attention of you
– Henrique.Araujo
I’m glad you solved it. If you want to post the solution here to help other people with the same problem, you can post it as a response, it’s part of the rules of the site. And as long as it’s a good solution, that answers what’s in the question, and teaches people right, you can get positive votes.
– Bacco