我试图设置一个全局常量,以后所有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上下文之外设置了这个变量吗?
发布于 2021-11-21 22:17:05
在查看Mezzio\ swoole \Command\StartCommand的源代码时,我发现我认为是我的php应用程序的入口点的index.php实际上根本没有被使用。
因此,常数确实是未定义的。
https://stackoverflow.com/questions/70035587
复制相似问题