I have a symfony 5 project.
And I faced a problem with autoloading.
I use default autoloading configuration, provided by the framework.
I have class, abstract class and interface defined in the same directory under the same namespace.
The problem is, when I describe my class like:
class MyClass extends MyAbstractClass
class MyAbstractClass implements MyClassInterface
I get autoloading error:
Attempted to load class "MyClass" from namespace "App\Entities".
Did you forget a "use" statement for another namespace?
A line from logs:
Error thrown while running command "myproject:mycommand". Message: "Class 'App\Entities\MyClass' not found" ...
If I use:
class MyClass extends MyAbstractClass
or
class MyClass implements MyClassInterface
or
class MyClass extends MyAbstractClass implements MyClassInterface
then the error is gone - everything works fine.
The error appears only if I use
class MyAbstractClass implements MyClassInterface
How to resolve the issue?
I need to make that abstract class would implement an interface, so any classes that will extend the AbstractClass would be compatible with the interface.
PHP 7.4.1 (cli)
The problem is the order, the files are being loaded.
I found out the problem, bit I did not fix it yet.
The problem is that the autoloader includes files in the alphabetical order, but not in the order, the files must be included.
My actual class names are:
AbstractEntity
EntityInterface
Ticket
Implementation is:
interface EntityInterface {}
abstract class AbstractEntity implements EntityInterface {}
class Ticket extends AbstractEntity {}
The files are included in the next order ( alphabetical ):
AbstractEntity.php
EntityInterface.php
Ticket.php
AbstractEntity.php
- this file declares the AbstractEntity
class.
This class implements EntityInterface
interface, so the EntityInterface.php
file must be already included before AbstractEntity.php
.
the correct order must be:
EntityInterface.php
AbstractEntity.php
Ticket.php
It is easy to check, by just renaming the AbstractEntity
to ObstractEntity
, then the alphabetical order will be:
EntityInterface.php
ObstractEntity.php
Ticket.php
For now it just left to fix the autoload order. But... How should it be fixed fast? Any ideas? Is it vendor autoload problem or Symfony?
AFAIK there is some kind of option in the composer.json
which states:
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
UPDATE : Alright, I've created a new project - and everything works well. It works both For Entity
directory and for Entities
. Versions all the same.
But once I renamed Entites
to Entity
in my source project - everything started to work with no errors.
So this is something strange thing.
I can only think that Symfony is not so flexible and the classes should be stored in Entity
directory then the autolaod works well.
For now it's
Ticket.php
AbstractEntity.php
EntityInterface.php
In the way they require each other.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.