문제

<?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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top