Open Qwoker opened 1 year ago
Okay, declare error i fixed with
if (!class_exists('test')) {
class test
{
...
}
now the question remains, how to initialize variables in init_worker so that they are visible and available for change in other workers, for example, in rewrite
Please, don't use class_exists()
, it isn't a solution to the problem.
The problem is basic knowledge of PHP.
If you include the same file two or more times in a php file, the error will always exist of cannot declare class (function, ...)
Try this in any PHP SAPI (php-fpm, cli, ...)
function rewrite() {
include "/etc/nginx/php/index.php";
}
rewrite();
rewrite();
....
Ngx-php is embeded PHP in Nginx, so the application is persistent. Once included a file, it isn't necessary to include again or you will have that problems.
A simple fix is use include_once
, or better because you will use it always, is a normal include
in the init_worker
.
The init_worker
only run the first time, and all the code is always available in any place in your workers.
The second error Class 'memcached' not found
, it's also normal.
You are calling a class: memcached
, that don't exist, because in your included file you only have the class: ngxphp\memcached\memcached
Check the files in the Techempower benchmark. Exist examples with global variables, with classes, .... https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/PHP/php-ngx
Yes, your right. If i include once on init_worker is good, work! Latest question how to collect data in memory PHP. Example do $dataArray = array() in init_worker and call it in content_by_php block and possible to modify $dataArray (unset, push and etc functions for arrays)
Hello! I try execute code on rewrite_block. My code config nginx
file index.php
file memchache.php i take here - https://github.com/rryqszq4/ngx-php-memcached
Log file error.log
I can't figure out how to connect php files correctly and make it so that every time the memory is not connected to the library. Maybe this can be done in Init_worker? How can you store data in php memory as it is done for example in Lua via a shared dict? So that I can get data not limited to the scope of the current process, but from another. Or is only memcached going to help here?