Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

khakimjanovich's avatar

php class_exists returns false even if the class exists

Hey there, i have a situation 😅. I created the package inside the package it has a migration file that adds rows to existing migration file. if I install the package it is now publishing that migration file even if the class exists. here is the code inside MyPackageProvider class

public function boot(): void
{
    $this->publishes([ $this->configPath() => config_path('myconfig.php')], 'config');

    if ($this->app->runningInConsole()) {
        if (!class_exists('AddFieldsToUsersTable')) {
            $this->publishes([
                __DIR__ . '/../../database/migrations/add_fields_to_users_table.php.stub'
                => database_path('migrations/' . date('Y_m_d_His', time()) . '_add_fields_to_users_table.php'),
            ], 'migrations');
        }
    }
}

as you can see I checked before I publish the migration. But class_exists function is not working as it is intended to work

0 likes
3 replies
SilenceBringer's avatar

@khakimjanovich you need to specify full namespace for AddFieldsToUsersTable class

if (!class_exists('\Namespace\AddFieldsToUsersTable')) {
1 like
sirch's avatar
if (!class_exists(AddFieldsToUsersTable::class)) {

That will generate the full namespace of the class

SomeOne01's avatar

This particular case might be a full class, but class_exists actually returns false when given an interface. In this case use interface_exists

Please or to participate in this conversation.