首页 > 解决方案 > 在 PhpStorm 中找到类但在运行时没有

问题描述

我正在使用 PhpStorm 开发 iCal Manager。

UserEngine用于所有网站的命名空间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";

我不使用自动加载。

标签: phpphpstormwamp

解决方案


我已经探索了@Á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");

我很高兴地说它有效,但恐怕这不是最好的解决方案,主要是因为如果类是在另一个目录中定义的,则此代码不起作用。


推荐阅读