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