PHP Composer PSR-4自动加载和子命名空间,未找到类 [英] PHP Composer PSR-4 Autoloading And Sub-Namespaces, Class Not Found

查看:239
本文介绍了PHP Composer PSR-4自动加载和子命名空间,未找到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是独立的,但我之前曾问过类似的问题:-



编辑器给出错误,未找到类



问题已解决,但我无法解释嵌套问题。我认为提出一个新问题会更合适。



我进行了很多搜索,但无法使嵌套名称空间与psr-4自动加载配合使用。 / p>

目录结构:-

 │composer.json 
│run.php

├───src
│├───一个
││parentclass.php
││
│└─ ──两个
│childclass.php

└───卖方
│autoload.php

└───作曲者
autoload_classmap.php
autoload_namespaces.php
autoload_psr4.php
autoload_real.php
ClassLoader.php
installed.json
许可

parentclass.php:-

 <?php 

命名空间myns\one;

抽象类父类
{
抽象公共函数abc();
}

childclass.php:-





 命名空间myns\two; 

命名空间myns\one;

使用myns\one\parentclass作为父类;

类子类扩展了父类
{
公共函数abc()
{
echo'hello world';
}
}

composer.json:-

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

run.php:-

 <?php 

require_once __DIR__。 ‘/vendor/autoload.php’;

使用myns\two\childclass作为childclass;

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

当我运行 php run.php 时。它给出错误:-

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


解决方案

一个类只能在文件中声明一个名称空间。通过在 childclass.php 中包含两个名称空间,您可能会覆盖第一个。



可以看到一个完整的示例此处使用多个名称空间,但文件仅包含1个命名空间声明。就是说,对于您的情况,我怀疑您只是犯了一个错误而只需要一个名称空间。



因为该文件位于 myns\two ; 您应该使用 namespace myns\two; 并删除另一个。


This question is independent but I did ask a similar question before:-

Composer Gives Error, "Class Not Found"

The problem was solved but I failed to explain the nesting issue. I thought it will be more appropriate to make a new question.

I searched a lot but I can't make the nesting namespaces to work with psr-4 autoloading.

Directory Structure:-

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

parentclass.php:-

<?php

namespace myns\one;

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

childclass.php:-

namespace myns\two;

namespace myns\one;

use myns\one\parentclass as parentclass;

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

composer.json:-

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

run.php:-

<?php

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

use myns\two\childclass as childclass;

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

When I run php run.php. It gives error:-

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

解决方案

A class can only declare one namespace in the file. By including two namespaces in childclass.php, you are likely overriding the first.

A full example can be seen here of using multiple namespaces, but the file only includes 1 namespace declaration. That said, I suspect for your case you simply made a mistake and only need one namespace.

Because the file is located in myns\two; you should use namespace myns\two; and remove the other.

这篇关于PHP Composer PSR-4自动加载和子命名空间,未找到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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