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

aligajani's avatar

ReflectionException thrown on composer update for a file that exists

To make this more interesting, things work just fine if I run composer dump-autoload -o but I am curious why would this throw an error when I run composer update in the first place? I need to get to the bottom of this. A quick fix doesn't make me happy internally.

aligajani at Alis-MBP in ~/Projects/saveeo on master ✗                                                                                    [faaba41c]  4:53
> composer update
> php artisan clear-compiled
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Package guzzle/guzzle is abandoned, you should avoid using it. Use guzzlehttp/guzzle instead.
Generating autoload files
> php artisan optimize

                                                                  
  [ReflectionException]                                           
  Class Saveeo\Board\Observers\BoardEventListener does not exist  

BoardEventListener.php (placed in Saveeo/Board/Observers)

<?php

namespace Saveeo\Board\Observers;

use Saveeo\Services\HashIds\Contracts\HashIds as HashIdService;

class BoardEventListener {

    private $hashIdService;

    public function __construct(HashIdService $hashIdService)
    {
        $this->hashIdService = $hashIdService;
    }

    public function whenBoardIsCreated($event)
    {
        $this->hashIdService->syncHashIdValueOnModelChanges($event, 'board');
    }

    public function whenBoardIsUpdated($event)
    {
        $this->hashIdService->syncHashIdValueOnModelChanges($event, 'board');
    }

    public function subscribe($events)
    {
        $events->listen(
            'Saveeo\Board\Observers\Events\BoardHasBeenCreated',
            'Saveeo\Board\Observers\BoardEventListener@whenBoardIsCreated'
        );

        $events->listen(
            'Saveeo\Board\Observers\Events\BoardHasBeenUpdated',
            'Saveeo\Board\Observers\BoardEventListener@whenBoardIsUpdated'
        );

    }

}

EventServiceProvider.php (placed in Saveeo/Providers)

<?php

namespace Saveeo\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        //
    ];

    /**
     * The subscriber classes to register.
     *
     * @var array
     */
    protected $subscribe = [
        'Saveeo\Board\Observers\BoardEventListener',
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events) {
        parent::boot($events);

        //
    }
}
0 likes
2 replies
Snapey's avatar

the problem is php artisan optimise... not composer

do you need to start your class use with a backslash?

    protected $subscribe = [
        '\Saveeo\Board\Observers\BoardEventListener',

or import it at the top of the file

if it is not that, check the letter case of all folder names and classname file

Please or to participate in this conversation.