Edit report at https://bugs.php.net/bug.php?id=69738&edit=1
ID: 69738
Updated by: jasny@php.net
Reported by: ugoren at interwise dot co dot il
Summary: Declare immutable variables
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Variables related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Please read https://wiki.php.net/rfc/howto
Previous Comments:
------------------------------------------------------------------------
[2015-06-04 06:16:20] ugoren at interwise dot co dot il
Regarding your question, the snippet you wrote should yield a syntax error on this line:
$b = &#a;//syntax error: Cannot reference immutable objects
And just to elaborate more on the immutable variables vs immutable values
concepts:
1. An immutable variable (#) cannot be assigned a mutable value.
2. And when assigning a mutable variable ($) the value of an immutable variable, the value would be
copied (and not referenced)
------------------------------------------------------------------------
[2015-06-04 01:32:07] cmb@php.net
Well, let's pretend the # syntax is no problem. But what about the
semantics of the "immutable variables"? Consider:
<?php
#a = [1,2];
$b = &#a;
$b[0] = 3;
var_dump(#a);
var_dump($b);
?>
What would be the result?
------------------------------------------------------------------------
[2015-06-03 05:25:20] ugoren at interwise dot co dot il
First of all, despite the fact that @ is used to suppress errors and warning, it could be used
(because variables are tokenized before the error-suppression character)
Second, it doesn't really matter... there are a lot of other characters that can be used to
declare new variables: ~ , !, %, * , ^
And even, a combination of characters, for example **
It's a very common syntax in other languages.
for example, in python:
def func (**kwargs):
and it is clear that the * in this context does not imply multiplication
------------------------------------------------------------------------
[2015-06-02 15:01:31] rasmus@php.net
Like which character? When you look through the parser you will find that there are very few
single-character operators available. @ is the error-suppression character.
------------------------------------------------------------------------
[2015-06-02 14:45:04] ugoren at interwise dot co dot il
You know, the # sign could be replaced with @ or any other character
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=69738
--
Edit this bug report at https://bugs.php.net/bug.php?id=69738&edit=1