|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69738 Declare immutable variables
Submitted: 2015-06-01 11:06 UTC Modified: 2019-09-27 13:05 UTC
From: ugoren at interwise dot co dot il Assigned:
Status: Not a bug Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2015-06-01 11:06 UTC] ugoren at interwise dot co dot il
I would like to have an option to declare immutable variables,
for example
$a=1;// is a mutable variable
#b=1;// is an immutable variable

The advantages of immutable variables are numerous:
1. caching function return values
2. parallelism (no shared memory)
3. pass variables by reference
4. Testability, just record a pure function's input and outputs, and you have automatically generated unit-tests.

Test script:
//sample code:
#a[1]=4;//error: modifying immutable variable

//pure functions:
function #pure_function(#a,#b)
   return #a+#b;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-01 11:13 UTC]
-Package: PHP Language Specification +Package: Variables related
 [2015-06-01 11:13 UTC]
The language specification describes the language as it is. This is a feature request against the language itself.
 [2015-06-01 12:45 UTC]
At first it should be noted that this syntax (# instead of $) is
not possible, because # starts a line comment (it's an alternative
to //).

| #a=array(1,2);
| #a=array(1,3);//ok
| #a[1]=4;//error: modifying immutable variable

If the second statement is allowed, I wouldn't say that the
variable is immutable, but rather that the value is immutable. It
is, however, already possible to have immutable values by using
immutable objects.
 [2015-06-02 06:51 UTC] ugoren at interwise dot co dot il
I was able to implement this feature myself:

I would love to see this feature implemented (and extended) in PHP7
 [2015-06-02 14:42 UTC]
And break millions of lines of code out there that use # comments? No chance.
 [2015-06-02 14:45 UTC] ugoren at interwise dot co dot il
You know, the # sign could be replaced with @ or any other character
 [2015-06-02 15:01 UTC]
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-03 05:25 UTC] 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-04 01:32 UTC]
Well, let's pretend the # syntax is no problem. But what about the
semantics of the "immutable variables"? Consider:

  #a = [1,2];
  $b = &#a;
  $b[0] = 3;
What would be the result?
 [2015-06-04 06:16 UTC] 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)
 [2019-09-27 13:05 UTC]
-Status: Open +Status: Not a bug
 [2019-09-27 13:05 UTC]
Please read
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Nov 29 00:01:26 2023 UTC