::gvar(1);
$a->a = 14;
$c = b::gvar(1);
echo $c->a; ?>
Note the $a = b::gvar(1) instead of $a = & b::gvar(1)!!!
Here is a good magazine article (PDF format) that explains the internals of PHP's reference mechanism in detail: http://derickrethans.nl/files/phparch-php-variables-article.pdf
It should explain some of the odd behavior PHP sometimes seems to exhibit, as well as why you can't create "references to references" (unlike in C++), and why you should never attempt to use references to speed up passing of large strings or arrays (it will make no difference, or it will slow things down).
It was written for PHP 4 but it still applies. The only difference is in how PHP 5 handles objects: passing object variables by value only copies an internal pointer to the object. Objects in PHP 5 are only ever duplicated if you explicitly use the clone keyword.
in additon to blakes comment. try this:
<?php
$strlen=60000;
$bstring=str_repeat("X",$strlen);
$astring=$bstring;
$starttime=microtime(true);
for($i=0;$i<=$strlen;$i++){
if($i==strlen($astring))
echo "End of String<br>";
}
echo (microtime(true)-$starttime)." seconds expired";
?>
and now change '$astring=$bstring;' to '$astring=&$bstring;'
on my system its 200 times slower with the reference.
increase $strlen and it gets even worse ;-)
(PHP 5.25 on UNIX)
A little gotcha (be careful with references!):
<?php
$arr = array('a'=>
7/24 首页 上一页 5 6 7 8 9 10 下一页 尾页 |