Composer提供错误,“未找到类” [英] Composer Gives Error, "Class Not Found"

查看:285
本文介绍了Composer提供错误,“未找到类”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows 10.在根目录中创建了一个文件夹 src 后,我在其中创建了两个文件。

I'm using Windows 10. After making a folder src in the root directory I created two files in it.

目录结构(在运行 composer install 之前):

Directory Structure (Before running composer install):

│
├── composer.json
├── run.php
│
└── src
     ├── childclass.php
     └── parentclass.php

根目录中的两个文件:

composer.json:

composer.json:

{
    "name": "myvendor/mypackage",
    "description": "nothing",
    "authors": [
        {
            "name": "Omar Tariq",
            "email": "XXXXX@gmail.com"
        }
    ],
    "require": {},
    "autoload": {
        "psr-4": {
            "myns\\": "src/"
        }
    }
}


$ b b

run.php:

run.php:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use myns\childclass as childclass;

$childclass = new childclass();
$childclass->abc();

src文件夹中的两个文件:

Two files in the src folder:

childclass.php:

childclass.php:

<?php

require_once 'parentclass.php';

use myns\parentclass as parentclass;

class childclass extends parentclass
{
    public function abc()
    {
        echo 'hello world';
    }
}

parentclass.php:

parentclass.php:

<?php

namespace myns;

abstract class parentclass
{
    abstract public function abc();
}

运行后的目录结构composer install

│
├── composer.json
├── run.php
│
├── src
│    ├── childclass.php
│    └── parentclass.php
│
└── vendor
     ├── autoload.php
     │
     └── composer
          ├── autoload_classmap.php
          ├── autoload_namespaces.php
          ├── autoload_psr4.php
          ├── autoload_real.php
          ├── ClassLoader.php
          ├── installed.json
          └── LICENSE

现在,当我运行:

php run.php

我得到这个错误:


致命错误:在第7行上的C:\wamp ... \run.php中找不到类'myns\childclass'

Fatal error: Class 'myns\childclass' not found in C:\wamp...\run.php on line 7


推荐答案

composer.json 中定义 src 文件夹使用 myns 命名空间,因此在 childclass.php 中应该使用

In composer.json you defined that for src folder you use myns namespace, so in your childclass.php you should use

namespace myns;

也不必添加:

require_once 'parentclass.php';

use myns\parentclass as parentclass;

因此您的childclass.php应如下所示:

so your childclass.php should look like this:

<?php

namespace myns;

class childclass extends parentclass
{
    public function abc()
    {
        echo 'hello world';
    }
}

此外在 php 文件,您可以替换:

In addition in run.php file you might replace:

use myns\childclass as childclass;

use myns\childclass;

您不需要将用作如果你不想为类使用其他名称。

You don't need to use as if you don't want to use other name for class.

您还应该考虑使用带大写字母(Studly caps)的命名空间和类。而不是 myns 使用 MyNs ,而不是 parentclass code> ParentClass 。您应该查看 PSR-1编码标准 PSR-2编码标准来遵循最佳编码实践。

You should also consider using namespaces with capital letter (Studly caps) and the same for classes. Instead of myns use MyNs, instead of parentclass use ParentClass. You should look at PSR-1 coding standard and PSR-2 coding standard to follow best coding practises.

这篇关于Composer提供错误,“未找到类”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆