Software Design Enthusiast And Engineer

Match Statement In PHP??

I initially thought of this idea a few months ago and I'm now deciding to put this out there in hopes that A) someone who writes C will contribute it to the PHP core, or B) It will be a continually reminder to myself to get it done!

So what's going on in that screenshot?

The idea I'm proposing here is that PHP objects be allowed to implement a (magic) __match method which would then allow them to be used in PHP's match statement.

The $value being passed in to the magic __match method would be the today() that's being passed to the match statement. Based on which day of the week "today" is, the logic in the appropriate match arm would run. And not only would that arm run, the on ($appoinment) declares the variable that can then be used on the right-hand side of the match statement.

This scenario is meant to represent a business' availability based on the day of the week. Which then allows the logic for allowing/denying the appointment to live outside of the appointment's purview. So in this example, the notifyUser method would be called based on what the reaction was inside the match statement.

As contrived as it is, this code gets the point across for what I'm trying to convey. Sure, the details could be a bit more planned out, but this would open all kinds of doors when it comes to monadic-based programming in PHP. A couple quick examples being the Option and Result monads from other functional languages. This __match method would go a really long way in writing more declarative code.

And as a final side note:

I've never heard mention in online communities of the fact that Laravel comes with the Option and Result monads baked-in! The vlucas/phpdotenv library uses them as a dependency. This, in turn, depends on the graham-campbell/result-type library. Together, these two libraries provide access to a couple of the most powerful tools used in functional programming.