Lifecycle
Process Lifecycle
- Each process has a long lifecycle.
- Each process runs independently and does not interfere with others.
- Each process can handle multiple requests during its lifecycle.
- A process will exit and end its lifecycle when it receives the
stop
,reload
, orrestart
command.
Tip
Each process is independent and does not interfere with others, which means that each process maintains its own resources, variables, and class instances, etc. This is reflected in that each process has its own database connection, and certain singletons are initialized once per process, resulting in multiple initializations across multiple processes.
Request Lifecycle
- Each request generates a
$request
object. - The
$request
object will be reclaimed after the request is processed.
Controller Lifecycle
- Each controller is instantiated only once per process, while multiple instances are created across multiple processes (with controller reuse disabled, see Controller Lifecycle).
- The controller instance will be shared by multiple requests within the current process (with controller reuse disabled).
- The controller lifecycle ends after the process exits (with controller reuse disabled).
About Variable Lifecycle
Webman is developed based on PHP, so it fully adheres to PHP's variable reclamation mechanism. Temporary variables generated in business logic, including instances of classes created with the new
keyword, are automatically reclaimed after functions or methods end, without needing to manually call unset
. In other words, the experience of developing with Webman is essentially consistent with traditional framework development. For example, in the following example, the $foo
instance will be automatically released upon the completion of the index method:
<?php
namespace app\controller;
use app\service\Foo;
use support\Request;
class IndexController
{
public function index(Request $request)
{
$foo = new Foo(); // Assuming there is a Foo class here
return response($foo->sayHello());
}
}
If you want a certain class instance to be reused, you can save the class in a static property of the class or in a long-lived object (such as a controller) property, or use the Container
's get
method to initialize the class instance, for example:
<?php
namespace app\controller;
use app\service\Foo;
use support\Container;
use support\Request;
class IndexController
{
public function index(Request $request)
{
$foo = Container::get(Foo::class);
return response($foo->sayHello());
}
}
The Container::get()
method is used to create and retain a class instance, which will return the previously created instance the next time it is called with the same parameters.
Note
Container::get()
can only initialize instances that do not have constructor parameters.Container::make()
can create instances with constructor parameters, but unlikeContainer::get()
,Container::make()
does not reuse instances, meaning it will always return a new instance even if called with the same parameters.
About Memory Leaks
In the vast majority of cases, our business code does not result in memory leaks (few users report memory leaks). We just need to be mindful not to let long-lived arrays grow indefinitely. See the following code:
<?php
namespace app\controller;
use support\Request;
class FooController
{
// Array property
public $data = [];
public function index(Request $request)
{
$this->data[] = time();
return response('hello index');
}
public function hello(Request $request)
{
return response('hello webman');
}
}
Controllers are long-lived by default (with controller reuse disabled), and the $data
array property of the controller is also long-lived. As requests to foo/index
continuously increase, the number of elements in the $data
array grows, leading to memory leaks.
For more related information, please refer to Memory Leak.