2

I would like to call a class without initializing it even if it's normally not static. So basically it should init itself with the first method call.

I'm wondering how laravel is doing this:

$someClass = SomeClass::where()->get()

This works on every method they provide.

I googled a long time but as long as you don't know the name of this behavior it's really hard to find.

Would be nice if you could help me out.

3
  • One way to find out would be to look in the Laravel source code. Commented Apr 3, 2015 at 22:57
  • You're right. I thought that it has a name so I wanted to know it. Because even if I knew how the're doing it, I wouldn't know the name of the behavior and couldn't look up some docs about it. Commented Apr 3, 2015 at 23:00
  • 1
    Yes, I didn't want to dismiss your question, but sometimes people forget that "open source" means that they can just look at how things work, so wanted to mention the possibility. Commented Apr 3, 2015 at 23:02

2 Answers 2

4

As it has been mentioned, it is all in the source code. Take a look at this part of the Illuminate\Database\Eloquent\Model class (Github link)

public static function __callStatic($method, $parameters)
{
    $instance = new static;
    return call_user_func_array(array($instance, $method), $parameters);
}

Every static method call for which no method exists (which is the case with most of the methods you can statically call on a model class) will end up at __callStatic.

Then, with new static, an instance of the class will be created and the method is called using call_user_func_array.

Sign up to request clarification or add additional context in comments.

Comments

2

Laravel (sometimes) calls this feature a "Facade" -- explaining how this is accomplished in Laravel means going way down the Laravel rabbit hole and is beyond the scope of a single Stack Overflow answer -- if you're interested in that I'm the author of a 10 article series that covers the implementation details of a lot of Laravel's "magic" methods -- worth checking out if you're the sort who likes those sort of nitty gritty details. The PHP manual entry on overloading is also useful.

The short version is, all PHP classes have a magic method named __callStatic. If your class has __callStatic defined

class Foo
{
    public function __callStatic($method, $args)
    {
    }
}

and you call a method that does not exist, or is protected/private

Foo::notThere(1,2,'foo');

Then PHP will call the __callStatic method instead, with $method being the method's name (notThere above) and $args being an array of parameters passed to the method (1, 2, and foo above)

5 Comments

Thank you very much! I'll accept your answer because you helped me with the term "facade" which I could lookup in the laravel docs.
Although they behave similarly I believe Eloquent Models aren't real Facades. It doesn't extend Facade and the only thing that's similar is the __callStatic part. Do you agree Alan?
@lukasgeiter That's correct, and why I added the the parenthetical (sometimes) :) Facades are classes that allow you to access a singleton service instance. Eloquent models are not facades, they're just model classes for eloquent's ORM and the static calling behavior doesn't seem to have a special name there. I included "facades" so would have something to google on.
@AlanStorm Exactly! However one tiny correction: The underlying instance of a facade doesn't have to be a singleton. It depends if it is registered via bind() or singleton(). (Even though most of the time you will use the same instance with multiple Facade calls, with or without singleton)
@lukasgeiter Right -- I was using the term singleton in the general sense, not in the "a Laravel application container singleton". From one point any object behind a facade becomes a singleton. Something something naming things is hard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.