CodeIgniter4: Bug: [Factories] when there are classes with the same short name, only the first class is loaded

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.

<?php

namespace App\Models;

use CodeIgniter\Model;

class Sample extends Model
{
    protected $table         = 'samples';
    protected $allowedFields = [];
}
<?php

namespace App\Models\Dir;

use CodeIgniter\Model;

class Sample extends Model
{
    protected $table         = 'dir_samples';
    protected $allowedFields = [];
}
<?php

namespace App\Controllers;

use App\Models\Dir\Sample as DirSample;
use App\Models\Sample;

class Home extends BaseController
{
    public function index()
    {
        $sample    = model(Sample::class);
        $dirSample = model(DirSample::class);

        var_dump($sample === $dirSample);
    }
}

The result is true.

Related #7684

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21 (19 by maintainers)

Most upvoted comments

In my opinion, if an FQCN is passed it should be just check only against class_exists and self::verifyInstanceOf and return. If a basename is passed (i.e. no namespace separators, like App), it should be checked if existing and satisfies the preferApp option when added the Config\ or App\Models\ namespace.