I'm not good in PHP and newbie in Kohana.
I'm very interesting how Kohana makes this structure: Request::current()->controller()
. We call static method and then call non-static method.
How I can organise similar structure?
Request::current()
will return an object, and you execute the controller()
method on that object.
I understand you're confused with the use of static and non-static at the same time. It isn't confusing at all. Given these two classes:
static class StaticClass
{
public function GetSomeObject
{
return new SomeObject();
}
}
class SomeObject
{
public function DoSomething()
{
// Do something
}
}
You can then initialize a SomeObject yourself, and execute its method:
$someObject = new SomeObject();
$someObject->DoSomething();
But it some cases this isn't desirable. I can imagine the StaticClass
being some kind of repository (a singleton or factory for example), managing your SomeObject
instances. You have to retreive all SomeObject
s from the StaticClass. In this example this is done like this:
$someObject = StaticClass::GetSomeObject();
You can then again call the method:
$someObject->DoSomething();
The latter two can be written at once like this:
StaticClass::GetSomeObject()->DoSomething();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.