深入理解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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.
