Riddle: $this<-code

../_images/make_it_work.png

Can you make this code work?

The challenge is to only add more code to this snippet, not less.

This syntax is valid PHP code, but one may expect $this->code, instead of $this<-code. Now, PHP is able to parse such syntax, and it then understands it as $this < -code. For this to run, we need code to be a global constant. This can be defined before the class itself, as const code = 2;. Constants may use lowercase, but this is not usual nor tradition.

Then, comparing to $this is not possible, although the left side of the operation is (string) $this. One only needs to turn $this to a string, and then compare it to a number. So, we can add the magic method __toString(), which returns a number in a string.

The final result is true or false, depending on the return of __toString().

In the end, the private property $code is unused. It was a bait.

4 AI were tested on this riddle: three managed to pass, one to fail. Can you?

See Also

PHP Features