變量引用是啥?php
多個變量同時使用一個內存空間,稱爲變量的引用。laravel
製做一個變量引用例子閉包
<?php $a = range(1,10); $b = &$a; $c = $b; xdebug_debug_zval('b'); //b: (refcount=2, is_ref=1)=array (0 => (refcount=0, is_ref=0)=1, 1 => (refcount=0, is_ref=0)=2, 2 => (refcount=0, is_ref=0)=3, 3 => (refcount=0, is_ref=0)=4, 4 => (refcount=0, is_ref=0)=5, 5 => (refcount=0, is_ref=0)=6, 6 => (refcount=0, is_ref=0)=7, 7 => (refcount=0, is_ref=0)=8, 8 => (refcount=0, is_ref=0)=9, 9 => (refcount=0, is_ref=0)=10)
refcount 計數被引用了2次。is_ref 是不是引用的 1/0debug
書寫引用變量能夠見減小內存佔用。code
摘一段laravel 代碼內存
<?php function(array $event) use (&$commands) { $commands[] = $event['started']->getCommandName(); }
經過引用傳參,達到閉包內數據修改,在包外正常使用處理過的數據。由於使用的同一塊內存地址get