PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Dec 16, 2019 1:25 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Dec 18, 2017 12:45 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
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 :)

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Last edited by VladSun on Tue Dec 19, 2017 7:41 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Mon Dec 18, 2017 3:17 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
Nice solution. Thanks for sharing.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Mon Dec 18, 2017 7:00 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
PostPosted: Mon Dec 18, 2017 10:50 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Jan 24, 2018 5:23 am 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group