質問

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