Thanks for the replies.
> > 3. What exactly does this mean: "Readonly variables inside loops are
> > permitted but limited to a single declaration"? What does a single
> > declaration look like?
>
> A readonly variable declared inside a loop is exposed to the outer scope,
> just like any other variable in PHP. This means that on the second iteration,
> PHP would attempt to re-assign an already initialized readonly variable,
> which results in a fatal error. A "single declaration" therefore means that
> the readonly variable can only be initialized once throughout the entire
> execution of the loop, not once per iteration.
So does that mean that it can only be used within a loop if that loop
only has a single iteration or that the assignment is guarded by some
condition?
$var = null;
while(true) {
if ($var === null) {
readonly $var = 42;
}
}
> > 5. You said that readonly variables cannot be used with the global
> > keyword. But what about readonly variables in global scope? Will they
> > still behave like a typical global variable?
>
> I am not entirely sure what you mean by "typical global variable behavior",
> so I will try to address what I believe you are asking.
> If your question is whether a readonly variable declared in the global scope
> can be imported into a function via the `global` statement, the answer is no.
> Importing a readonly variable via `global` is forbidden and results in an
> error.
> If you are asking whether readonly variables declared in the global scope are
> automatically accessible in function scopes, the answer is also no. Readonly
> variables follow the same scoping rules as regular variables, meaning they
> are not automatically available inside functions, just as regular variables
> are not.
> If you meant something else entirely, please feel free to clarify and I will
> be happy to address it.
I meant using $GLOBALS.
readonly $var = 42;
function foo() {
$GLOBALS['foo'] = 'bar';
}
foo();
var_dump($foo);
> > 6. What exactly counts as a variable declaration? When is it a valid
> > syntax to use this keyword? I don't think PHP had the concept of
> > variable declaration until now.
>
> By "declaration" I simply mean a regular variable assignment prefixed with
> the readonly keyword:
>
> ```
> <?php
> readonly $foo = "bar”;
> ```
>
> You are right that PHP has not had a formal concept of variable declaration
> until now. The readonly keyword in this context is purely a modifier on a
> standard assignment statement, signaling that the variable cannot be
> re-assigned after its initial value has been set. Any valid assignment
> expression can be used as the initial value, including object instantiation,
> function calls, or scalar values.
Right, but if PHP users have never thought about variable declaration,
then it might be confusing for them to understand when it is legal to
use this keyword. For example, you said that array destructuring is
not supported but for me it's a regular variable assignment. It seems
that assignment within a loop is also not allowed, but that doesn't
seem to be clearly specified in the RFC.
while(readonly $arr = $pdoStmt->fetch()) {} // Is this allowed
assuming it only iterates once?
foreach($arr as readonly $element) {} // This is forbidden, correct?
for(readonly $foo; ; ) {} // Is this allowed?
if(readonly $val = foobar()) {} // This is a regular variable
assignment, is this allowed?
What about array vivification?
readonly $arr[] = 42;
Or a declaration with a warning:
readonly $i++;
What about a slightly more complex variable assignment?
$arr = [readonly $var = 42];
// or
readonly $var1 = $var2 = 42;
// or
foobar(readonly $baz = 42);
// or
$foo = 'bar';
readonly $$foo = 42;
Regards,
Kamil