Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

mutable php and python

comparing
php
to
python

which variables are mutable

What does this mean in each language
what is the purpose in each language
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

"Mutable" means "changeable."  In PHP all variables of the main scope are mutable (IMHO a great shortcoming of the language).  Inside a function, you have a separate scope that gives you the ability to make these variables immutable to the outside code.  Except for the superglobal variables and variables declared "global."  Inside a class definition, the visibility and therefore the mutability of the variables is controlled by the public, protected and private declarations, with the default (unfortunately) being public.  

After that, you might want to learn about namespaces.  Start your adventures here:
http://php.net/manual/en/language.namespaces.php
Avatar of rgb192

ASKER

main scope are mutable

is this the global scope

or the local scope in a method


which means that the other is not mutable?
By the "main scope" I mean any variable that is defined outside of a function or method.  I think that is the same as Global Scope in PHP.
http://php.net/manual/en/language.variables.scope.php
Avatar of rgb192

ASKER

So all global scope variables can be modified in a method and outside the class

But local scope variables can only be modified in the method?
Yes, that's apparently correct.  Example here, but nobody would write code like that.  The goal of variable scope is to ensure that your variables are not available for reading or writing outside a sensible context.  This reduces the risk of variable name collisions and enables two or more programmers to work on the same project simultaneously.  The existence of the global scope is an artifact of the early history of PHP, when computer science took a back seat to ease of use.

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

Class Thing
{
    public function __construct()
    {
        global $a;
        $a = 3;
    }
}

$x = new Thing;
var_dump($a); // PRINTS "int(3)"

Open in new window

Also, be aware of object properties which have their own visibility according to the rules of public, protected and private.  Unlike global vs functional scope, visibility errors trigger noticeable failures, making it much easier to detect and correct malformed code.
Avatar of rgb192

ASKER

https://www.experts-exchange.com/viewCodeSnippet.jsp?refID=40212835&rtid=20&icsi=1

so you created an example where the local variable line9 is actually a global variable
and because line9 is now global, it is mutable?
Yes. Try this, for example.  But please don't think this is good coding practice -- it is not.  I'm only proving a point to address your question; this is not something you want to do!

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

Class Thing
{
    public function __construct()
    {
        global $a;
        $a = 3;
    }
    public function get_a()
    {
        global $a;
        return $a;
    }
}

$x = new Thing;
var_dump($a); // PRINTS "int(3)"

// MUTATE $a
$a = 4;
$b = $x->get_a();
var_dump($b); // PRINTS "int(4)"

Open in new window

Avatar of rgb192

ASKER

public function get_a()
    {
        global $a;
        return $a;
    }

so a must be called global again in order to have value of 4?
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

so
$this->a
is always local?


could we do
global $this->a
$this->a=3
$this->a is not really a "local" because the object-oriented syntax does not distinguish between private, protected, and public.  They are all called $this-> something.  If you omit $this-> you get a local variable, one defined only in the scope of the method.  Unless you use the global keyword, which nobody would ever want to do inside a class method.

I have never tried it (feel free to make your own experiment) but global $this->a should be a parse error.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's not a term limited to any specific programming language; it's an industry concept.  Here are the dictionary definitions:
http://dictionary.reference.com/browse/mutable?s=t
http://dictionary.reference.com/browse/immutable?s=t

Here is some discussion on the concept.
http://en.wikipedia.org/wiki/Immutable_object

Here is a PHP example:
http://php.net/manual/en/class.datetimeimmutable.php
Avatar of rgb192

ASKER

Local and global examples are good because it is easier to change global

if the method designates it then the x variable can be immutable, and can NOT be valued changed.

Please understand that python represents all its data as objects (unlike php), , so it was kind of necessary to have the terms mutable and immutable


So now I think of python data as php objects

Thanks.