Is there any way to negate result of callback?
$this->myInjectedService->doSomething([$this, 'myCallback']);
I need to nagate result of myCallback method. I know I can do it by nested function, but is there any more clean way (exclamation mark)?
$this->myInjectedService->doSomething(function() {
return !$this->myCallback();
});
No, there is no direct way to do it like that.
What you could do, would be to create a callback wrapper which negates the callback you want to call. Every time you want to negate a callback you can use that wrapper instead of the callback itself:
The results would be:
Please note that this is the simplest approach I could think of, but if you need more complex callbacks to be called then you might need to update the wrapper.
PS: Even if it's possible, I don't recommend using this approach, as using a nested function, as you already did, is much more clear and easier to read/understand.