找不到带有PHP命名空间的Class

Jas*_*vis 26 php namespaces

我之前发布了一些关于在PHP中使用命名空间的问题以及我得到的问题,我在下面的示例代码应该正常工作.

但是当我尝试在PHP中使用Namespace时,我遇到了错误.以下是运行下面的代码时出现的第一个错误...

Fatal error: Class 'Controller' not found in E:\Controllers\testing.php on line 6
Run Code Online (Sandbox Code Playgroud)

E:\ Controller\testing.php文件

<?php
use \Controller;

include('testcontroller.php');

$controller = new Controller;
$controller->show();
?>
Run Code Online (Sandbox Code Playgroud)

E:\ Controller\testcontroller.php文件

<?php
use \Library\Registry;

namespace Controller
{
    class Controller
    {
        public $registry;

        function __construct()
        {
            include('E:\Library\Registry.class.php');
            $this->registry = new Registry;
        }

        function show()
        {
            echo $this->registry;
            echo '<br>Registry was ran inside testcontroller.php<br>';
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

E:\ Library\Registry.class.php文件

<?php
namespace Library\Registry
{
    class Registry
    {
        function __construct()
        {
            return 'Registry.class.php Constructor was ran';
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我试图让它变得尽可能简单,只是为了让Namespace部分工作.我尝试了不同的变化,似乎无法弄明白.

Tim*_*mur 36

即使使用usestatement,也需要指定要尝试实例化的类的命名空间.这里有很多例子:http://www.php.net/manual/en/language.namespaces.importing.php

为了更好地理解它,我将向您描述它是如何工作的.在您的情况下,当您这样做时,您可以use \Controller使用整个Controller命名空间,但不能使用此命名空间中的类.所以,例如:

<?php
include('testcontroller.php');

use \Controller;

// Desired class is in namespace!
$controller = new Controller\Controller();

// Error, because in current scope there is no such class
$controller = new Controller();

$controller->show();
?>
Run Code Online (Sandbox Code Playgroud)

另一个例子:

testcontoller.php:

<?php
namespace Some\Path\To\Controller;

class Controller
{
    function __construct()
    {

    }

    function show()
    {
        echo '<br>Was run inside testcontroller.php<br>';
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

testing.php:

<?php
include('testcontroller.php');

use \Some\Path\To\Controller;

// We now can access Controller using only Controller namespace,
// not Some\Path\To\Controller
$controller = new Controller\Controller();

// Error, because, again, in current scope there is no such class
$controller = new Controller();

$controller->show();
?>
Run Code Online (Sandbox Code Playgroud)

如果您希望完全导入Controller ,则需要执行use Controller\Controller- 然后可以在当前范围内访问此类.

  • 很好的例子,我昨晚想出了这个,但很适合你描述它。因此,我认为我最好将所有“库类”放入与“库”相同的命名空间中,然后在上面的测试控制器上,我可以对库中我需要的每个类使用“use Library\Classname”控制器,这样我就可以像 http://www.php.net/manual/en/language.namespaces.importing.php 上的“新注册表”等一样评估它们,我认为这是在说“导入全局”类`所以我不确定这是否是一个坏主意 (2认同)

Kin*_*nch 9

命名命名空间并不是一个好主意,比如类,因为它令人困惑(我认为这就是这里发生的事情).您可以通过use Controller此引用将别名定义为类\Controller或命名空间\Controller,但是您的类因为它位于命名空间内而被命名为\Controller\Controller 1

use Controller;
$class = new Controller\Controller;
Run Code Online (Sandbox Code Playgroud)

要么

$class = new \Controller\Controller;
Run Code Online (Sandbox Code Playgroud)

要么

use Controller\Controller;
$class = new Controller;
Run Code Online (Sandbox Code Playgroud)

我们的想法是,当您尝试使用其相对名称访问类时,它会尝试将"第一部分"映射到使用定义的任何别名use(remeber与之use MyClass相同use MyClass as MyClass.后面的内容as是别名).

namespace MyNamespace\MyPackage\SomeComponent\And\So\On {
  class MyClass {}
}
namespace Another {
  use MyNamespace\MyPackage\SomeComponent; // as SomeComponent
  $class =              new SomeComponent\An\So\On\MyClass;
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,PHP找到SomeComponent第一部分并将其映射到SomeComponent-alias上面的行.

您可以在有关命名空间手册中阅读有关它的更多信息.

1如果您使用其完整名称命名一个类,则称其为"完全限定类名".


Dan*_*oap 6

当您将一个类Controller放在命名空间中时Controller,您必须以这种方式引用它:

$controller = new Controller\Controller();
Run Code Online (Sandbox Code Playgroud)

\Controller 将是全局(默认)命名空间中的一个类,即好像根本没有使用任何命名空间.

  • @Dan,改变`$ this-> registry = new Registry;`到`new\Library\Registry\Registry`确实摆脱了错误.必须要有办法做我想做的事情,你知道吗?"使用"的一点是避免这种情况 (3认同)
  • 这是因为您是在全局名称空间中而不是在使用它的名称空间中引用注册表。您的课程也实际上称为Library \ Registry \ Registry (2认同)