php.net |  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: 2015-06-04 06:16 UTC
From: ugoren at interwise dot co dot il Assigned:
Status: Open Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-06-01 11:06 UTC] ugoren at interwise dot co dot il
Description:
------------
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=array(1,2);
#a=array(1,3);//ok
#a[1]=4;//error: modifying immutable variable

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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-01 11:13 UTC] chx@php.net
-Package: PHP Language Specification +Package: Variables related
 [2015-06-01 11:13 UTC] chx@php.net
The language specification describes the language as it is. This is a feature request against the language itself.
 [2015-06-01 12:45 UTC] cmb@php.net
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:

http://stackoverflow.com/questions/30570763/replace-the-in-php-variable-declaration/30588938#30588938

I would love to see this feature implemented (and extended) in PHP7
 [2015-06-02 14:42 UTC] rasmus@php.net
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] 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-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] 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-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)
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun May 19 16:01:26 2019 UTC