Return an instance of the same class in Static Function

Asked

Viewed 91 times

1

I’m trying to return an instance of the same class in static functions, for example, in the code below, works normal:

class Router
{
    private $name;

    public function get()
    {
        return $this;
    }

    public function name($name) {
        $this->name = $name;
    }
}

And to use:

$router = new Router();
$router->get()->name('string');

But when I try to static function, nothing happens.

class Router
{
    private static $name;

    public static function get()
    {
        return self::class;
    }

    public static function name($name) {
        self::$name = $name;
    }
}

And to use:

Router::get()->name('string');

With static return receipt:

Fatal error: Uncaught Error: Call to a Member Function name()

  • You don’t know what you intended to happen, you both do nothing, because that’s what these classes do. https://ideone.com/OoCL0n Anyway it doesn’t make much sense to create a code like this, it would have to explain better where you want to get.

  • @Maniero, I edited the question, see if you can understand.

  • The question has changed completely, but the text still deals with some of the old way and has become confused, but I will answer anyway because I think I can understand despite being poorly formulated.

1 answer

2


To call static members you should always use ::, not only use in one place, then the correct code would be:

Router::get()::name('string');

Obviously it is possible to return the class itself in a static method and use the call the way you wrote. But then the method to be called to be instance rather than static. It is all married, the way it declares the method determines how it should be called.

class Router {
    private static $name;

    public static function get() {
        return $this;
    }

    public function name($name) {
        self::$name = $name;
    }
}

I put in the Github for future reference.

But a code like that doesn’t make sense.

  • So, I was looking at Laravel, they wear something similar to: Route::get()->name();. So I can do it my way name could not be static ? Do you recommend some reading on the practices on this ? In my view I do not think it right to mix everything.

  • Just for the record here, this way didn’t work either, the way without being Static just return $this, with Static does not work :/

  • Miner, return new static; so I can access the class methods; it worked now. Thank you

  • Route::get() in your example you need to return an instance, not a self::class

  • @gmsantos, in this case return new static; right?

  • 2

    The get() yes, the name() no. Of course returning $this" does not give error, I answered on top of what posted. I recommend a structured study and not be trying to guess how things are. I recommend starting with the basics before practicing (practice is good when you know what you are doing). But today almost no one wants to do that, and so most people who are starting out are deluded that they are learning to program because they can do some things. You seem at least to be trying to understand what is going on, but you need to structure it. Look for books.I do not indicate by not knowing good ones

Show 1 more comment

Browser other questions tagged

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