codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: model() function behavior with classes that has same short name #7710

Closed WCrash7 closed 1 year ago

WCrash7 commented 1 year ago

PHP Version

8.2

CodeIgniter4 Version

4.3.6

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

cli-server (PHP built-in webserver)

Database

No response

What happened?

model() function returns same class when passing classes with different namespace and similar short name.

Steps to Reproduce

Store "LicenseModel ":


namespace App\Models\Store;

use App\Models\BaseModel;

class LicenseModel extends BaseModel
{

}

User "LicenseModel ":


namespace App\Models\User;

use App\Models\BaseModel;

class LicenseModel extends BaseModel{

}

Test:

namespace App\Controllers\User;

use App\Controllers\BaseController;
use App\Models\Store\LicenseModel as StoreLicense;
use App\Models\User\LicenseModel as UserLicense;

class UserAuth extends BaseController{
    public function login()
    {
        echo "Store Model: " . model(StoreLicense::class)::class ."\n";
        echo "User  Model: " . model(UserLicense::class)::class;
        //return $this->customRender(view("pages/login"));
    }
}

Result: Store Model: App\Models\Store\LicenseModel User Model: App\Models\Store\LicenseModel

Expected Output

Store Model: App\Models\Store\LicenseModel User Model: App\Models\User\LicenseModel

Anything else?

No response

neznaika0 commented 1 year ago

Use options as temporary fix model(UserLicense::class, false);

kenjis commented 1 year ago

Duplicate of #7694

If you have opinion, please post in #7694.

kenjis commented 1 year ago

Dose model(UserLicense::class, false) work?

Try:

model('User/LicenseModel');
WCrash7 commented 1 year ago

Dose model(UserLicense::class, false) work?

Try:

model('User/LicenseModel');

Yes, They both work as temporary fixes