深入理解PHP对象赋值
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> <br> 2</span> //<span style="color: #000000;"><span style="color: #000000;">深入理解PHP对象赋值</span> </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">echo</span> '<pre class="brush:php;toolbar:false">'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> StdClass; </span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$obj</span>->name = 'Pig'<span style="color: #000000;">; </span><span style="color: #008080;"> 7</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=> string(3) "Pig" }</span> <span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #800080;">$copy</span> = <span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $obj ,$copy都是new StdClass返回的同一个标识符的拷贝</span> <span style="color: #008080;">10</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$copy</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #800080;">$objRef</span> = &<span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 此时会将$obj转换成引用,然后赋值给$objRef,因此$obj,$objRef都为引用 </span> <span style="color: #008080;">13</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$objRef</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #800080;">$objClone</span> = <span style="color: #0000ff;">clone</span> <span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;">新空间</span> <span style="color: #008080;">16</span> <span style="color: #800080;">$obj</span>->name = 'After Clone'<span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$objClone</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">18</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(11) "After Clone" } </span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #008000;">//unset是删除引用效果</span> <span style="color: #008080;">21</span> <span style="color: #800080;">$nameRef</span> = &<span style="color: #800080;">$obj</span>->name; <span style="color: #008000;">//</span><span style="color: #008000;">$obj->name被转换成引用(& string),然后赋给$nameRef</span> <span style="color: #008080;">22</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#2 (1) { ["name"]=>&string(11) "After Clone" }</span> <span style="color: #008080;">23</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$nameRef</span>); <span style="color: #008000;">//</span><span style="color: #008000;">删除引用</span> <span style="color: #008080;">24</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>string(11) "After Clone" } </span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> <span style="color: #008000;">//null是赋值效果 </span> <span style="color: #008080;">27</span> <span style="color: #800080;">$nameRef</span> = &<span style="color: #800080;">$obj</span>->name; <span style="color: #008000;">//</span><span style="color: #008000;">恢复name的引用</span> <span style="color: #008080;">28</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#2 (1) { ["name"]=>&string(11) "After Clone" }</span> <span style="color: #008080;">29</span> <span style="color: #800080;">$nameRef</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">; </span><span style="color: #008080;">30</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#2 (1) { ["name"]=>&NULL }</span> <span style="color: #008080;">31</span> <span style="color: #008080;">32</span> <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$objRef</span>); <span style="color: #008000;">//</span><span style="color: #008000;">仅仅删除了引用</span> <span style="color: #008080;">33</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>); <span style="color: #008000;">//</span><span style="color: #008000;">object(stdClass)#1 (1) { ["name"]=>&NULL }</span> <span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #800080;">$objRef</span> = &<span style="color: #800080;">$obj</span>; <span style="color: #008000;">//</span><span style="color: #008000;">恢复对象引用</span> <span style="color: #008080;">36</span> <span style="color: #800080;">$obj</span>->name = 'Lucy'<span style="color: #000000;">; </span><span style="color: #008080;">37</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">null</span>; <span style="color: #008000;">//</span><span style="color: #008000;">赋值$obj为null,$obj只是new StdClass的标识拷贝,不会影响其内容。 </span><span style="color: #008080;">38</span> <span style="color: #008000;"> //$objRef做为$obj的引用,会同时被赋值null </span><span style="color: #008080;">39</span> <span style="color: #008000;"> //等价 $objRef = null;</span> <span style="color: #008080;">40</span> <span style="color: #008080;">41</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj</span>,<span style="color: #800080;">$copy</span>,<span style="color: #800080;">$objRef</span>,<span style="color: #800080;">$objClone</span><span style="color: #000000;">); </span><span style="color: #008080;">42</span> <span style="color: #008000;">//</span><span style="color: #008000;"> NULL, </span><span style="color: #008080;">43</span> <span style="color: #008000;">// object(stdClass)#1 (1) { ["name"]=>&string(4) "Lucy" }, </span><span style="color: #008080;">44</span> <span style="color: #008000;">// NULL, </span><span style="color: #008080;">45</span> <span style="color: #008000;">// object(stdClass)#1 (1) { ["name"]=>string(3) "Pig" }</span> <span style="color: #008080;">46</span> <span style="color: #008080;">47</span> ?>

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
