Solved

mutable php and python

Posted on 2014-07-20
15
461 Views
Last Modified: 2014-07-24
comparing
php
to
python

which variables are mutable

What does this mean in each language
what is the purpose in each language
0
Comment
Question by:rgb192
  • 7
  • 7
15 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40207783
"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
0
 

Author Comment

by:rgb192
ID: 40212564
main scope are mutable

is this the global scope

or the local scope in a method


which means that the other is not mutable?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40212706
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
0
 

Author Comment

by:rgb192
ID: 40212776
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?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40212835
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.
0
 

Author Comment

by:rgb192
ID: 40212947
http://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?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40213046
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

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rgb192
ID: 40213105
public function get_a()
    {
        global $a;
        return $a;
    }

so a must be called global again in order to have value of 4?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 40213169
It must be called global in any context in which $a would be used.  In this case, it is global in the constructor, and must be called global again in the get_a() function.  Otherwise it will be given the scope of the get_a() function only.  And of course, this is different if we make it a property by referring to it as $this->a.
0
 

Author Comment

by:rgb192
ID: 40213332
so
$this->a
is always local?


could we do
global $this->a
$this->a=3
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40213941
$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.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 350 total points
ID: 40215341
greetings rgb192, , there seems to be much confusion here about the TERM you use in your question as -
     mutable

Your question is ill formed, because in PHP there are NO programming TERM use as mutable, so you can NOT use this statement -
      "What does this mean in each language
       what is the purpose in each language"

as there are NO reference to a mutable in the PHP language, none, it is not used.

However in python language, which has very different "thinking" and programming syntax (the code methods used) than php has to offer. In python language, the terms mutable and immutable are applied,  mutable  is used to designate that if you pass a variable (say var x) to the parameters of a function or class method then it's value CAN be changed IN the method -
x = 5
print x // outputs 5
func(x)
print x  // outputs 9, changed in the function so it's mutable

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

This is because in python (unlike php) all variables are passed by reference, so they are generally considered to be mutable, however because some variable types in python are more complex, then they are immutable to the extent normally thought of, HOWEVER in code you can modify the value, BUT instead of using the previous Object in the variable (and change the value), instead it deletes the previous object and creates a NEW OBJECT, with a NEW VALUE using the same variable name like x .
Sorry but in my view there is NO comparison for php and python for programming language learning.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40215515
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
0
 

Author Closing Comment

by:rgb192
ID: 40215989
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.
0
 

Author Comment

by:rgb192
ID: 40215990
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
php mail headers 2 14
how to split this type of line? 5 57
Creating a slider 12 35
Web Reply Form - PHP with Upload 4 22
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now