mercredi 14 octobre 2015

Is it a bad practice to consider an interfaced parameter as a concrete one?

I have the following classes :

<?php

class SaveEvent implements EventInterface
{
    private $_path;

    public function __construct($path) { $this->_path = $path; }
    public function getName() { return 'save'; }
    public function getPath() { return $this->_path; }
}

class Observer implements ObserverInterface
{
    public function update(EventInterface $event)
    {
        switch ($event->getName()) {
            case 'save':
                $path = $event->getPath();
                file_put_contents($path, 'File saved');
                break;
            case 'quit':
                // Other event
        }
    }
}

As you can see, my "update" method takes in parameter an EventInterface and according to the kind of event will trigger a specific process. It's about the same implementation than SplSubject / SplObserver but instead of passing my subject to my observers I will pass an event (that eventually can contain data).

My observer will listen for a "save" event, event linked to a file path, and will stored the string "File saved" on this path.

Is it a bad practice to accept an "interfaced" parameter, check its concrete type and to consider it like its concrete type after?

If yes which solution do you propose to solve this problem?

PS: I'm not satisfied on title and tags I set for this topic, if you have better please help me :)

Aucun commentaire:

Enregistrer un commentaire