PHP Developers Network
http://forums.devnetwork.net/

Current PHP callables considered bad partice
http://forums.devnetwork.net/viewtopic.php?f=19&t=145021
Page 1 of 1

Author:  VladSun [ Mon Dec 18, 2017 12:45 pm ]
Post subject:  Current PHP callables considered bad partice

Several times we had issues with unit testing (and later in production code) classes that use callbacks - it's always the callback signature messed up while calling it. Unit tests could not help (at least not in a nice way) because the callbacks were mocked. Also the mocking of the callback is kind of an PITA.

It was pretty obvious that we need , but somehow it was declined (?!?).
So, eventually our team members agreed on that callables won't be used ever and "callable" interfaces will be created and required instead. Later, as PHP evolved, the where introduced which made our code much more readable.

Syntax: [ Download ] [ Hide ]
interface ITimerCallback {
  public function onTick(ITimer $timer);
}

interface ITimer {
  public function tick(ITimerCallback $handler);
}

...

$this->tick(new class implements ITimerCallback {
  public function onTick(ITimer $timer) {
    echo $timer->getTime();
  }
});

 


Just sharing :)

Author:  Christopher [ Mon Dec 18, 2017 3:17 pm ]
Post subject:  Re: Current PHP callables considered back partice

Nice solution. Thanks for sharing.

Author:  requinix [ Mon Dec 18, 2017 7:00 pm ]
Post subject:  Re: Current PHP callables considered back partice


Author:  Christopher [ Mon Dec 18, 2017 10:50 pm ]
Post subject:  Re: Current PHP callables considered back partice


Author:  VladSun [ Wed Jan 24, 2018 5:23 am ]
Post subject:  Re: Current PHP callables considered back partice


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/