может кто-то из пхп-интерналистов объяснить, как референсы работают? Я тут голову себе морочил пол-часа, пока понял, что схема значительно страшнее и запутанней жавской. Примеры:<!-- 1 -->
<?php
$i = array('foo');
$j = &$i;
#unset($i);
$i = array('ku');
print_r($j);
?>печатает array('ku') . Аналогично для классов. Я считал, что все объекты в ПХП есть умные указатели, операция =& копирует указатель, т.е. по идее $i есть указатель на массив, он копируется в $j, затем в $i загоняется новый указатель (что и должен делать new во всех нормальных языках). Оказалось, нет...
да, чуть не забыл: 4.3.6
http://www.php.net/manual/ru/language.variables.php
>http://www.php.net/manual/ru/language.variables.php
[excuses for my English, got problmes with ru kbd map]everybody read it but it doesn't explain the script behaviour. Looks like PHP uses java-like references frontend, but pointers-style backend, e.g. there's more than one level of references
What do you expect from the script in first post? $j variable just points to $i. As manual says about references:
"References in PHP are a means to access the same variable content by different names. They are not like C pointers, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The most close analogy is with Unix filenames and files - variable names are directory entries, while variable contents is the file itself. References can be thought of as hardlinking in Unix filesystem."
>What do you expect from the script in first post? $j variable
>just points to $i.I expect both $i and $j to be one-level references, though any constructor (new Object(), or internal like array()) puts a pointer (a reference to another table entry) into the variable. As you see, $j is a pointer to $i and $i is a pointer to an array, so we've got 2 levels of pointers. Strange thing, isn't it?
Look at your code example:
<?php
$i = array('foo'); //initializing varibale $i
$j = &$i; /* passing value of $i to $j by reference (same variable contnetn by different name) In other words PHP references allow you to make two variables to referto the same content. */
#unset($i);
$i = array('ku'); // setting new value to $i
print_r($j); /* printing $j, but $j points to CONTENT of $i and tent of $i has changed */
?>
If you familar with unix file system and hard linking, you should be clear about references in PHP. It's not Java, it's scripting language :)