Codeception/AspectMock Parent class not found by locator

后端 未结 2 887
我寻月下人不归
我寻月下人不归 2021-02-19 16:41

I have a problem with Codeception/AspectMock. When using custom autoloader and try to create an instance of a class which has parent form the same custom namespace I have this

2条回答
  •  走了就别回头了
    2021-02-19 17:18

    If you can easily do a find and replace on your codebase, maybe you could refactor your code to PSR-4 autoloading standards and do away with the need for a custom autoloader altogether.

    This is the spec https://www.php-fig.org/psr/psr-4/. I'll try and explain it as simply as possible.

    Imagine changing your lowercase namespace lib to Lib, and setting that namespace to the src/ directory in your composer.json:

    "autoload": {
        "psr-4": {
          "Lib\\": "src/"
        }
    }
    

    After setting that, run composer dumpautoload. Then all you need to do is search and replace namespace lib;, replacing with namespace Lib;.

    An example class located in src/Form.php would have namespace Lib; at the top, followed by class Form.

    Namespaces use the folder naming convention. All classes directly in src/ have namespace Lib;. If there are subdirectories, the directory name becomes part of the namespace. For example a file in src/Form/Field/Text.php would have namespace Lib\Form\Field; class Text {}.

    You can see the full convention in the link above, but the general rule is make any folders begin with a capital letter, as with your classname, and the autoloader should be able to find all of your classes.

    This is probably the best practice solution for you, and again as I said, only requires a little bit of file renaming and namespace tweaking. Good luck!

提交回复
热议问题