Learn how to properly import PHP libraries
P粉214089349
2023-09-03 17:07:32
<p>我正在使用两个库:</p>
<ul>
<li>phpdotenv</li>
<li>spatie/async</li>
</ul>
<p>这是我正在执行的内容:</p>
<pre class="brush:php;toolbar:false;"><?php
require_once "/var/local/entrop/vendor/autoload.php";
include_once '/var/local/entrop/inc/Functions.php';
include_once '/var/local/entrop/model/Contract.php';
use Spatie\Async\Pool;
try {
$aConn = Functions::getConnection();
$contracts = Contract::getContracts();
$pool = Pool::create();
foreach ($contracts as $contract){
$pool->add(function () use ($aConn, $contract) {
include_once '/var/local/entrop/config/DataConfig.php';
$next_contract = Contract::getNextContract($aConn);
})
->then(function ($output) {
})
->catch(function ($exception) {
Functions::write_log($exception);
})
->timeout(function () {
Functions::write_log("timeout");
});
}
await($pool);
$aConn->close();
}
catch (Exception $e){
Functions::write_log($e);
}</pre>
<p>这是DataConfig的定义:</p>
<pre class="brush:php;toolbar:false;"><?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once "/var/local/entrop/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::createImmutable("/var/local/entrop/");
$dotenv->load();
define('DB_HOST', $_ENV['DB_HOST']);
define('DB_USER', $_ENV['DB_USER']);
define('DB_PASS', $_ENV['DB_PASS']);
define('DB_DBMS', $_ENV['DB_DBMS']);
class DataConfig {
static $db_host = DB_HOST;
static $db_user = DB_USER;
static $db_pass = DB_PASS;
static $db_dbms = DB_DBMS;
}</pre>
<p>这是我的composer.json文件:</p>
<pre class="brush:php;toolbar:false;">{
"require": {
"vlucas/phpdotenv": "^5.2",
"spatie/async": "^1.5"
},
"autoload": {
"classmap": [
"/var/local/entrop/model/Contract.php"
]
}
}</pre>
<p>现在,我遇到的错误是:</p>
<blockquote>
<p>[2022-08-04 10:38:38]local.INFO:空间\异步\输出\并行错误:
PHP 注意:未定义索引:DB_HOST in
/var/local/entropia/config/DataConfigEntropia.php 第 12 行 PHP
注意:未定义索引:DB_USER in
/var/local/entropia/config/DataConfigEntropia.php 第 13 行 PHP
注意:未定义索引:DB_PASS in
/var/local/entropia/config/DataConfigEntropia.php 第 14 行 PHP
注意:未定义索引:DB_DBMS in
/var/local/entropia/config/DataConfigEntropia.php 第 15 行</p>
</blockquote>
<p>我尝试的方法是自动加载我的DataConfig文件,并将其包含在回调函数中。但没有起作用。我应该怎么做?</p>
phpdotenv is poor at error reporting (and I personally think it also doesn't rely properly on standards and best practices, which can lead to a number of side effects, including the caveats you encountered - it's just that I'm fairly sure their authors thought this is a feature rather than a bug, so I have to admit I won't judge them, at the end of the day, users need to work on themselves and understand what they are doing).
For your specific problem, if you use a library, learn how to configure it. Even my previous comment might prompt you that it might not be suitable, actually - and this is where phpdotenv shines - it's doing most of the heavy lifting and you can control most of the parts --- just not the file format.
Your problem is not the file format. So configure your application correctly to take parameters from any environment and handle error conditions (for example, throwing on unexpected values at the beginning). This should also reveal what mistakes you made when using this or that library, and fix it in your code.