-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: [Factories] when there are classes with the same short name, only the first class is loaded #7694
Comments
Of course we can load the second class if we use the relative path: $dirSample = model('Dir/Sample'); But it cannot specify the namespace. |
I don't understand the reasoning behind this basenaming logic. This very logic causes the bug in the Debug Toolbar where passing See 6f8f3a2 |
@paulbalandan I think it is for implementing preferApp feature. |
In my opinion, if an FQCN is passed it should be just check only against |
@paulbalandan That is easy to understand, and it does not surprise devs. But the current behavior is intentional from the begininng. See #5356 If we change it, it will be a big change. |
@MGatner Why do you need preferApp ? The current behavior is that if a developer happens to have a class in preferApp assumes that there are no classes with the same short name, which seems uncertain. We don't know which namespace |
I found another bug or unexpected behavior with preferApp. #7699 |
Who can explain in a nutshell why we need the model() function? UserModel::model() === UserModel::model() Or a function that will store instances of objects. singleton(Some::class) === singleton(Some::class) |
I think the solution here is to introduce a check for parent classes. So |
The function is just a convenient wrapper for Factories. It functions similarly to a singleton but the power of it is to allow third-party modules to be extended natively. So if |
@MGatner I could be wrong, but it seems to me that most of those who encounter the problem voiced here are using the model() function as a class loader from CI 3 (i.e. $this->load->model()), and not the ability to replace models for third party modules. What you're talking about, I think it should be solved like this. // in module
model(\Acme\Some::class); // alias \Acme\Some::class for class \Acme\Some::class
// in app
model(\Acme\Some::class, \App\MyClass::class); // alias \Acme\Some::class for class \App\MyClass::class Any subsequent call to |
@MGatner Who uses the feature to override the third-party models? I think the specification that if there happens to be a class in App with the same short name, |
I'm fine with both of these suggestions. This was developed alongside Myth:Auth where we were going through a whole lot of effort to allow developers to bring their own models. Instead of using a lot of publishing scripts and Config files for model lookup we added the instance App preference. As @iRedds notes this is a sort of stand-in for a service container, which is largely the point of Factories to begin with. |
|
Yep. Shield does it too, though not sure if we have docs on it explicitly there. |
Shield has User Provider, and there is no docs on Factories. |
I've noticed this issue as well. The way we can avoid BC is by modifying the way basename works. If just class base name is passed the whole discovery stuff should be triggered so basically it should work the way it works now. But if FQCN is passed the basename should cache by FQCN so that classes with same name from different namespace can be used. Here is my example where i noticed the issue use Pages\Models\Page\SitemapModel as PageModel; model(PageModel::class) With current setup second model() call will return the cached instance of Pages\Modes\Page\SitemapModel which is not good behaviour. So idea is to cache FQCN if FQCN is passed but if we call model('SitemapModel') then the whole discovery and regular basename caching should work as is. |
I implemented like that in #7733 |
#7733 is completely fine The issue with #7733 is that it introduces the BC. I was thinking about implementing quick non BC change for the next patch version so that model() can start behaving properly immediately until there is a merge window for the #7733 in the next minor release. I'm just not sure if doing that without BC is even possible. |
This bug will be fixed in 4.4.0 by #7733. @najdanovicivan This behavior has been in place since the beginning and is not urgent enough to be fixed in 4.3.x. |
Closed by v4.4.0. |
When there are classes with the same short name, and we try to load them with Factories by passing the full classname, only the first class is loaded.
There is no way to load the second class when specifying the full classname.
The result is
true
.Related #7684
The text was updated successfully, but these errors were encountered: