Cannot find Class with PHP Namespace

前端 未结 5 1744
深忆病人
深忆病人 2020-12-09 08:31

I posted some questions previously regarding the use of Namespaces in PHP and from what I got, this example code I have below should be working.

However I am getting

5条回答
  •  攒了一身酷
    2020-12-09 08:55

    Even when using use statement, you need to specify the namespace of the class you are trying to instantiate. There are a lot of examples here: http://www.php.net/manual/en/language.namespaces.importing.php

    To understand it better, I will describe to you how it works. In your case, when you do use \Controller, the whole Controller namespace becomes available to you, but not the classes that are in this namespace. So, for example:

    show();
    ?>
    

    Another example:

    testcontoller.php:

    Was run inside testcontroller.php
    '; } } ?>

    testing.php:

    show();
    ?>
    

    If you wish to import exactly the Controller class, you need to do use Controller\Controller - then this class will be accessible in your current scope.

提交回复
热议问题