通过xdebug来分析PHP引用
一直没搞懂php的引用,今天面试的时候又碰到了,借助xdebug貌似理解了一点,记录一下。
?
code1:
?
$a = "xiaoshenge";$b = &$a;unset($b);echo "b=$b";echo "a=$a";
结果:b=a=xiaoshenge
?
code2:
$a = "xiaoshenge";$b = &$a;unset($a);echo "b=$b";echo "a=$a";
结果:b=xiaoshengea=??
?
猜测:
?
面试的时候是code1,由于没搞懂PHP的引用当时就猜测的写了b=a=,其实这与我把引用跟c里面的指针搞混了有关。回来之后,调试了一下code2,然后彻底迷惑了,于是求救与PHP文档。
?
PHP文档中对于引用的介绍:
?
?
引用是什么:在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文件本身——变量名是目录条目,而变量内容则是文件本身。引用可以被看作是 Unix 文件系统中的 hardlink。
?
取消引用:当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
?
<?php $a = 1;$b =& $a;unset($a);?>
?不会 unset?$b,只是?$a。再拿这个和 Unix 的?unlink?调用来类比一下可能有助于理解。
?
推断:
?
文档里面有介绍”引用可以被当作是unix文件系统中的hardlink“,参考http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=150986?中的介绍:
硬连接是给文件一个副本,同时建立两者之间的连接关系。修改其中一个,与其连接的文件同时被修改。如果删除其中[color=red]任意一个[/color]其余的文件将不受影响。?
软连接也叫符号连接,他只是对源文件在新的位置建立一个“快捷(借用一下wondows常用词)”,所以,当源文件删除时,符号连接的文件将成为无源之水->仅仅剩下个文件名了,当然删除这个连接,也不会影响到源文件,但对连接文件的使用、引用都是直接调用源文件的。?
?
通过xdebug来看zval容器中的变化:
?
code1:
?
$a = "xiaoshenge";$b = &$a;xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );unset($b);xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );
结果:
?
a:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
b:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
a:
<em>(refcount=1, is_ref=0)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
?
code2:
?
$a = "xiaoshenge";$b = &$a;xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );unset($a);xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );
?结果:
a:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
b:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
b:
<em>(refcount=1, is_ref=0)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
?
?
结合unix的硬链接来看(应该如下):
unset($a),只是销毁了a-x部分所以$b还在。
?
$b=&$b,不是指针那样,$b指向$a,(指针,貌似我的C都还给书了,要补习了)
?
面试感受:不能人云亦云,要自己动手,自己思考。PHP手册不能轻视,这个问题文档里面有。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
