Good day dear% username%
That appeared on Habré such a wonderful topic
Difference Javascript from PHP;) did not read, but I do not
judge I do not know what it was all over all the comments could not reread, but I would like to dispel a couple of errors and inaccuracies that were made by
so, let's begin
"By default, everything is passed by value, and objects too
In PHP, they tried to build into absolute the concept of the C language, that everything is transmitted by value. And they initially did it even for objects, but then changed the concept a bit - objects are still values, but this value is a link to an instance of a class. ”- false, in PHP objects are passed only by reference.
Objects and references (% username% do I need to sort through why this is so - is there a need for a separate topic dedicated to the method of transferring objects. I don’t want to make a stupid topic-question about this first comment on this post will be a question “is a separate topic dedicated to transfer of objects "if yes then plus, if not set minus)
further example of the author
PHP:<?php $arr = array( 'key1' => 'value1', 'key2' => 'value2', ); function doSmthWithArray($arr) { $arr['key3'] = 'value3'; } doSmthWithArray($arr); print_r($arr);
Javascript: var arr = { key1: 'value1', key2: 'value2' }; function doSmthWithArray(arr) { arr['key3'] = 'value3'; } doSmthWithArray(arr); console.log(arr);
So the example is more than incorrect. Why? Well, in the first place in JavaScript, almost everything is an object.
“JavaScript is almost entirely object-based. JavaScript objects are associative arrays, augmented with prototypes (see below). Object property names are string keys: obj.x = 10 and obj ['x'] = 10 are equivalent, it is syntactic sugar. Properties and values can be added at run-time. Most properties of an object chain can be enumerated using a for ... in loop. JavaScript has a small number of built-in objects such as Function and Date. ” Wikipedia tells us
So
var arr
full-fledged object passed by reference.
')
In our favorite PHP, the type of
array
not an object. (How many types are there in PHP? The
answer is here ) but as you and I only know
object
passed by reference, everything else is by value. That is why in the author’s example the source array remains unchanged. Is it possible to cast an array to an object? Yes
<?php $arr = array( 'key1' => 'value1', 'key2' => 'value2', ); $ArrayObject = (object) $arr; var_dump($ArrayObject); $anotherArrayObject = new ArrayObject($arr); var_dump($anotherArrayObject);
Either we explicitly convert to the type object or use the built-in class
ArrayObject in the first case
$ArrayObject
becomes an object whose properties are the keys of the original array, and the values of these properties are the values of the array. In the second case, our array has become an object of type ArrayObject which you can familiarize with by the link above. And now when our array has become an object, let's launch an example of the author of the original article with modifications.
$arr = array( 'key1' => 'value1', 'key2' => 'value2', ); $arr = new ArrayObject($arr); function doSmthWithArray($arr) { $arr['key3'] = 'value3'; } doSmthWithArray($arr); print_r($arr); echo $arr['key3'];
Now run this code and look at the result! Well, what are the objects transmitted in PHP by reference?
ArrayObject Object ( [storage:ArrayObject:private] => Array ( [key1] => value1 [key2] => value2 [key3] => value3 ) ) value3
Then the author of the original article goes this:
"Copy-on-write
This is definitely not everyone knows about it. In short: at least in PHP and everything is transmitted by value, but, in fact, copying is not always. It occurs when the value changes inside the structure, as, for example, was in the first example. But this is not the only time a copy is made. More on this link. "I do not quite understand what it is about. We have already found out that simple types are passed by value to objects by reference.
All read up to this place many thanks. I hope I explained a bit of confusion with the transfer of objects by reference. Yes, do not forget about the mini vote. For any inaccuracies, errors, unpleasant design, I will forgive forgiveness in advance and even if nobody needs this topic :)
UPD1: Gentlemen is the topic of the answer to the
Difference of Javascript from PHP;)UPD2: Minus what exactly you do not agree? In that in PHP the object is transferred on the link?
UPD3: Maybe I put it wrong but
Passing a parameter by valuePassing a parameter by value means that the caller copies into the memory available to the callee (usually the stack) the immediate value.
Changing a copy of a variable, respectively, does not affect the original.Passing parameter by referencePassing a parameter by reference means that it is not the value itself that is being copied, but the address of the original variable.
Passing by reference allows you to avoid copying all the information describing the state of the object.
Changing the state of a parameter passed by reference leads to changes in the original object.Thus, all I wanted to say is that objects are already passed by reference (yes, in fact, we get a transmission by value of an
address in memory, by which the original object can be found ) But let's reduce everything to the following simple type is passed by value (
Change copies of the variable, respectively, the original does not affect. )
, object by reference (
Changing the state of a parameter passed by reference entails changes to the original object. )
$test = 'LOREM IPSUM'; function byValue($param) { $param = strtolower($param); } byValue($test); echo $test.PHP_EOL; $object = new stdClass(); function byReference($param){ $param->foo = 'bar'; } byReference($object); var_dump($object);
I have questions in person, I’ll answer everything, I just can’t work as I do now. I will answer as I am released thanks.