douzhang6176 2014-03-18 02:56
浏览 27
已采纳

使用SplClassLoader.php和名称空间

<?php
    //The basic Code.
    require_once("SplClassLoader.php");
     $loader = new SplClassLoader('test', 'lib');
        $loader->register();

    use test\database\Connector;
    ?>

    <?php
    namespace test\database;
    class Database {

    }
    ?>


    <?php
    namespace test\database\Connector;
    class Connector extends text\database\Database {

    }
    ?>

my file structure looks like this

\

-index.php

-splclassloader.php

\test

\test\database

\test\database\Database.php

\test\database\Connector.php

Its not loading the connector class. What is it that I don't understand here.

  • 写回答

1条回答 默认 最新

  • dongtiao2066 2014-03-18 03:18
    关注

    If you are trying to autoload classes that are mapping folders to namespaces (I'm assuming that is the case) make sure that your namespaces only include the folder names and omit class names. The use statement, on the other hand, would not omit the class name.

    Using the namespace of test\database\Connector and then defining a class Connector means that you would instantiate the object using:

    $connecter = new \test\database\Connector\Connector();
    

    I'm guessing you have an additional subnamespace Connector that isn't needed. Namespace test\database\Connector should be test\database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog