I am currently developing in laravel and one of the things that I have seen when using eloquent is the chaining that they do with other example methods
$table = Table::all()->where('name','Alex');
How is it possible that the method that is magical is chained with a static method? I've seen the CoR (Chain of Responsibility) pattern and so far I've only seen them do it with magic methods
you have to have everything armed with its namespace and the method in theory has to return
self::${innstancia_de_la_clase}
I leave you an explanation of how it is done:First you need any class in which you declare a private static property and in turn a public static method that validates and returns the instance y
self
:In this way when executing:
FooClass::
your loader resolves the access to the class but when executinggetInstance()
you are returning the instance; As you are returning the instance, it allows you to directly access a chain method that follows it and that self has access to, for example,fooMethod()
in this case the call would look like this:the output:
I also learned how to do it recently, but with normal methods:
Documentation
Maybe there could be another more elegant way but for me it was the best and easy to understand.
For my concept they are not magic methods when making strings with static; For me a magic method would be something like:
When you are substituting the parts of a code for dynamic variables, this has its problems with IDEs and that is that they lose portability... it is not recommended... Avoid it...