Current PHP callables considered bad partice

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Current PHP callables considered bad partice

Post by VladSun »

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 callable prototypes, 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 anonymous classes where introduced which made our code much more readable.

Code: Select all

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 :)
Last edited by VladSun on Tue Dec 19, 2017 6:41 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Current PHP callables considered back partice

Post by Christopher »

Nice solution. Thanks for sharing.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Current PHP callables considered back partice

Post by requinix »

VladSun wrote:It was pretty obvious that we need callable prototypes, but somehow it was declined (?!?).
PHP discusses everything publicly on their internals mailing list. Whenever you want to know what's going on, reading an archive of the emails is a great place to start.

AFAIK callable_types was supported in concept but had issues. Such as how it would be allowable to craft incredibly complex function signatures

Code: Select all

function foo(int $a, int $b, callable(callable(callable(callable(int, int):int $zebranky, int):int $pik, int):int $fot, int):int $zot): int {
    return $zot($a, $b);
}
And that default values in the signature would not be allowed

Code: Select all

function foo(callable(int = 123) $fn) {
And that it could create problems with strict_types and parameter variance.

In the rest of the OOP world, your problem is solved by interfaces or strict typing.
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Current PHP callables considered back partice

Post by Christopher »

requinix wrote:In the rest of the OOP world, your problem is solved by interfaces or strict typing.
And indeed, the solution VladSun posted is to use interfaces...
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Current PHP callables considered back partice

Post by VladSun »

requinix wrote:In the rest of the OOP world, your problem is solved by interfaces or strict typing.
How To Save The Princess In 8 Programming Languages

Just for fun, no rant :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply