On 26.08.24 11:26, Bilge wrote:
Thanks for this question; I find this super interesting because it's something we haven't thought about yet. I must admit I completely overlooked that, whilst an interface /can/ require implementers to specify a default, in the case that they do not, it is still valid for implementations to selectively elect to provide one. Therefore I can append to your example the following case (I removed the `string` return type for now): class ZipCompression implements CompressionInterface { public function compress(string $data, int $level) { var_dump($level); } } new GzipCompression()->compress('', default ?? 6); new ZipCompression()->compress('', default ?? 6); In this case, we get the following output: int(4) Fatal error: Uncaught ValueError: Cannot pass default to required parameter 2 of ZipCompression::compress() I would like to fix this if possible, because I think this should be valid, with emphasis on /if possible/, because it may be prohibitively complex. Will update later.
That would be a way to fix it, to basically make isset(default) a possible check if there is no default, similar to an undefined variable check. It would also recognize a default value of null as not set in the same way, so one could not differentiate between null and not defined, but that is in line with the language in general.