php - 在 PhpStorm 中找到类但在运行时没有
问题描述
我正在使用 PhpStorm 开发 iCal Manager。
我User
在Engine
用于所有网站的命名空间User
中定义了一个类,在命名空间中定义了一个iCal
仅用于 iCal 进程的类,从\Engine\User
.
编码时,PhpStorm 可以定位\Engine\User
以定义\iCal\User
,但执行时出现“致命错误:找不到类 \Engine\User”。
我在 WampServer 上使用 PhpStorm 2019.1 和 PHP 7.2.4。
我试着做一个别名,没有用。我试图将我的所有文件合二为一,然后将其添加到“引擎”中,但也没有用。
\引擎\用户:
<?php
declare(strict_types=1);
//Created for IGPD by leroy the 27/07/2019
namespace Engine;
use PDO;
use Exception;
use PDOStatement;
/**
* Class User
* @property bool attendee
* @package Engine
*/
class User
{
public function __construct(array $user)
{
$this->email = $user['email'];
$this->name = $user['name'];
$this->surname = $user['surname'];
$this->language = $user['language'];
}
}
\iCal\用户:
<?php
declare(strict_types=1);
//Created for IGPD by leroy the 27/07/2019
namespace iCal;
use Engine\User as UserAlias;
/**
* Class User
* @package iCal
*/
class User extends UserAlias
{
public function __construct(array $user, string $cutype, string $role, bool $rsvp, string $partstat, string $sentby, string $dir, string $type)
{
$this->calAddress = "mailto:" . $user['email'];
$this->cutype = in_array($cutype, ['INDIVIDUAL', 'GROUP', 'RESOURCE', 'ROOM', 'UNKNOWN']) ? $cutype : 'UNKNOWN';
$this->role = in_array($role, ['CHAIR', 'REQ-PARTICIPANT', 'OPT-PARTICIPANT', 'NON-PARTICIPANT']) ? $role : 'OPT-PARTICIPANT';
$this->rsvp = $rsvp ? 'TRUE' : 'FALSE';
$this->cn = $user['name'] . " " . $user['surname'];
$this->language = in_array($user['language'], ['EN', 'FR']) ? $user['language'] : 'EN';
$this->partstat = in_array($partstat, ['NEEDS-ACTION', 'ACCEPTED', 'DECLINED', 'TENTATIVE', 'DELEGATED']) ? $partstat : 'NEEDS-ACTION';
$this->sentby = $sentby;
$this->dir = $dir;
$this->type = $type;
UserAlias::__construct($user);
}}
我只是运行类文件来检查错误,所以我不应该得到任何东西。实际输出为:
致命错误:第 12 行的 D:\wamp\www\IGPD\iCal\User.php 中找不到类“Engine\User”
这是iCal.php
,需要目录中所有文件的文件:
\iCal\iCal.php:
<?php
declare(strict_types=1);
namespace iCal;
//Created for IGPD by leroy the 02/08/2019
require_once 'Alarm.php';
require_once 'Calendar.php';
require_once 'Event.php';
require_once 'FreeBusy.php';
require_once 'Journal.php';
require_once 'Todo.php';
require_once 'User.php';
然后我在我的主页中使用了它,但它不是我想要加载的,所以我不需要它:
\引擎\引擎.php:
<?php
declare(strict_types = 1);
namespace Engine;
session_start();
require_once "collections.php";
require_once "..\iCal\iCal.php";
我不使用自动加载。
解决方案
我已经探索了@ÁlvaroGonzález 提到的自动加载解决方案,到目前为止它是唯一可行的解决方案。结果,我在index.php
我的目录的根目录中提出了这段代码。
\index.php:
<?php
declare(strict_types=1);
namespace iCal;
spl_autoload_register(function ($class){
require_once ucfirst($class) . '.php';
});
$e = new Calendar("e", "e", "e", "r", "e");
$f = new User(['email' => "", 'name'=>"", 'surname'=>"", 'language'=>""], "e", "e", true, "e", "e", "e", "e");
我很高兴地说它有效,但恐怕这不是最好的解决方案,主要是因为如果类是在另一个目录中定义的,则此代码不起作用。
推荐阅读
- php - Twig 在生产环境中截断我的数据
- html - 将媒体查询从最大宽度反转为最小宽度
- sql - 是否可以将 CHARACTER 转换为 INT
- python - Scrapy SitemapSpider在干净关闭后不继续
- sql-server - 将时间从 1530 转换为下午 3:30 时间格式 SQL Server
- algorithm - 如何使用滚动、俯仰和偏航更新 GPS 坐标?
- excel - 遍历工作簿中的所有工作表,并将存储为文本的数字更改为数字
- visual-studio-2019 - 如何在 Visual Studio 2019 - Live Share 帐户中更新密码?
- html - 视口之外的背景颜色?
- google-maps - 如何用谷歌地图构建这个产品?