Re: [RFC] Default expression

From: Date: Mon, 26 Aug 2024 10:40:49 +0000
Subject: Re: [RFC] Default expression
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 26/08/2024 08:28, Rowan Tommins [IMSoP] wrote:
As far as I can see, nobody has actually justified reading values out in this way, only said it's a side-effect of the current implementation. It's pretty useful for testing.
Aside: one of those examples brings up an interesting question: is the value pulled out by "default" calculated only once, or each time it's mentioned? In other words, would this create 3 pointers to the same object, or 3 different objects? foo(array|Something $x=new Something); foo([default, default, default]);
Thanks for this interesting question. Here is the output from your (slightly modified) script: class Something {} function foo(array|Something $x=new Something) {return $x;} var_dump($x = foo([default, default, default])); var_dump($x[0] === $x[1]); array(3) {   [0]=>   object(Something)#1 (0) {   }   [1]=>   object(Something)#2 (0) {   }   [2]=>   object(Something)#3 (0) {   } } bool(false) As you can observe from the object hashes, each object is unique. That is, we fetch the default value each time the keyword appears, which in the case of objects, creates a new instance each time. I will update the RFC with this information. Cheers, Bilge

Thread (101 messages)

« previous php.internals (#125263) next »