Help with $this and self in php

Asked

Viewed 56 times

-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?

  • 2

    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.

  • 1

    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".

  • 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.

  • 2

    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.

  • 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) )

  • 1

    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.

  • 1

    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.

  • 1

    already solved my problem, the tableless guys knew me understand and helped me in 2 min. without complication. obg by attention of you

  • 1

    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.

Show 4 more comments

2 answers

2

You can solve this by simply keeping it the way you’re already doing

$u = new User();
$u->find([]);

The $this cannot be statically accessed. So it will not work to do User::find([]);

It is possible to do what you want, but not recommended.

If the intention is merely visual, to have a smaller code, forget it. Or if the intention is to provide an access inline, can think of another way method chaining or simply create a function:

function userFinD()
{
    $u = new User();
    return $u->find([]);
}

userFinD();

But if you are bothered to have to create a function, you can also call inline a multi-line routine. This is done using anonymous functions.

As I am not sure what you really intend to do, I will avoid detailing the options cited.

  • obg for your help, but I decide to use another method for that my purpose. got even better. anyway obgd!

-2

HICCUP:

abstract class BaseModel {
    public function find($params){
        $table = '';

        if($this)
            $table = $this->$table;
        else {
            $u = new User();
            $table = $u->table;
            unset($u);
        }

        //...
    }
}

class User extends BaseModel {
    protected $table = "users";
}
  • 4

    I don’t understand where that solves what was asked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.