我试图设置一个全局常量,以后所有 php swoole 进程和线程都可以访问它,但它似乎不可见或其他东西
这是我的 index.php - 几乎是香草 mezzio - 我只看到APPROOT常量:
(function () {
if (!defined('APPROOT')) {
define('APPROOT', __DIR__);
}
/** @var \Psr\Container\ContainerInterface $container */
$container = require 'config/container.php';
/** @var \Mezzio\Application $app */
$app = $container->get(\Mezzio\Application::class);
$factory = $container->get(\Mezzio\MiddlewareFactory::class);
// Execute programmatic/declarative middleware pipeline and routing
// configuration statements
(require 'config/pipeline.php')($app, $factory, $container);
(require 'config/routes.php')($app, $factory, $container);
$app->run();
})();
但是当我尝试在其中一个中间件中引用/使用该常量时出现错误:
<?php
declare(strict_types=1);
namespace Application\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class BootstrapMiddleware implements MiddlewareInterface
{
public function __construct(private ResponseFactoryInterface $responseFactory)
{
}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$this->setAssetsCompiledLoc();
$response = $handler->handle($request);
return $response;
}
private function setAssetsCompiledLoc()
{
if ( ! defined('ASSET_MAP')) {
$manifestPath = \APPROOT . '/manifests/manifest-' . ENV . '.json';
// ...blah...
}
}
}
我收到此错误:
错误未定义常量“APPROOT”
为什么?那是因为 swoole 上下文之外的所有变量都被丢弃了,而我在 swoole 上下文之外设置了这个?