rgb192
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
php
to
python
which variables are mutable
What does this mean in each language
what is the purpose in each language
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
http://php.net/manual/en/language.variables.scope.php
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?
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)"
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.
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?
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)"
ASKER
public function get_a()
{
global $a;
return $a;
}
so a must be called global again in order to have value of 4?
{
global $a;
return $a;
}
so a must be called global again in order to have value of 4?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so
$this->a
is always local?
could we do
global $this->a
$this->a=3
$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.
I have never tried it (feel free to make your own experiment) but global $this->a should be a parse error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Local and global examples are good because it is easier to change global
So now I think of python data as php objects
Thanks.
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.
ASKER
I have a related followup question about the answer.
https://www.experts-exchange.com/questions/28482748/variables-objects-passed-by-reference-value.html
https://www.experts-exchange.com/questions/28482748/variables-objects-passed-by-reference-value.html
After that, you might want to learn about namespaces. Start your adventures here:
http://php.net/manual/en/language.namespaces.php