Alexander Solonik
Alexander Solonik

Reputation: 10230

Php autoloading not working in symfony dispatcher example

I am trying to just check out the symfony event dispatcher class and i have been following this online tutorial , so i have the following in my index.php file:

<?php


require('vendor/autoload.php');

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher;

$dispatcher->addListener('UserSignedUp' , function(Event $event){
    // die('Handling It !!');

    var_dump($event);

}); 

$event = new App\Events\UserSignedUp( (object) [ 'name' => 'gautam' , 'email' => '[email protected]' ] );


$dispatcher->dispatch('UserSignedUp' , $event);

and i have the following directory structure:

  event_dis
    - app
       - events
         - UserSignUp.php
    - vendor
    - index.php
    - composer.json

I have the following in my composer.json file:

{
    "require": {
        "symfony/event-dispatcher": "^3.2"
    },

    "autoload" : {
        "psr-4" : {
            "App\\" : "app/"
        }
    }

}

The UserSignedUp.php class looks like the following :

<?php

namespace App\Events;


class UserSignedUp extends Event {
    public $user;
    public function __construct($user) {
        $this->user = $user;
    }

}

Now since i have the following line in my index.php file:

$event = new App\Events\UserSignedUp( (object) [ 'name' => 'gautam' , 'email' => '[email protected]' ] );

The UserSignedUp class gets called and i get the following error in my browser:

Class 'App\Events\Event' not found in C:\xampp\htdocs\symfony_compo\event_dis\app\Events\UserSignedUp.php on line 6

Now why am i getting this error , in the tutorial this same example works perfectly, but on my local machine this does't , can somebody tell me what am i doing wrong here ??

Upvotes: 0

Views: 751

Answers (1)

rap-2-h
rap-2-h

Reputation: 31948

Event class does not exists in App\Events namespace. You should edit UserSignedUp.php and add use Symfony\Component\EventDispatcher\Event;:

<?php

namespace App\Events;

use Symfony\Component\EventDispatcher\Event;

class UserSignedUp extends Event {
    public $user;
    public function __construct($user) {
        $this->user = $user;
    }
}

Upvotes: 2

Related Questions