In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "PHP object replication example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "PHP object replication example Analysis"!
Prototype pattern can be regarded as an important part of object replication. When we learn the prototype pattern, we learn that when the reference variable in the object, that is, the variable is also an object, copying the object directly will cause the reference variable in it to point to the same object. Is it a bit of a twist? let's use an example to illustrate:
/ / clone method
Class testA {
Public $testValue
}
Class A
{
Public static $reference = 0
Public $instanceReference = 0
Public $t
Public function _ construct ()
{
$this- > instanceReference = + + self::$reference
$this- > t = new testA ()
}
Public function _ clone ()
{
$this- > instanceReference = + + self::$reference
$this- > t = new testA ()
}
}
$A1 = new A ()
$a2 = new A ()
$A11 = clone $A1
$A22 = $a2
Var_dump ($A11); / / $instanceReference, 3
Var_dump ($A22); / / $instanceReference, 2
$A1-> t-> testValue = 'now it's A1'
Echo $a11-> t-> testValue, PHP_EOL; / /''
$a2-> t-> testValue = 'now it is a2'
Echo $a22-> t-> testValue, PHP_EOL; / / is now a2
$A22-> t-> testValue = 'now it's A22'
Echo $a2-> t-> testValue, PHP_EOL; / / is now A22
/ / after using clone
$A22 = clone $a2
Var_dump ($A22); / / $instanceReference, 4
$a2-> t-> testValue = 'now it is a2'
Echo $a22-> t-> testValue, PHP_EOL; / / NULL
$A22-> t-> testValue = 'now it's A22'
Echo $a2-> t-> testValue, PHP_EOL; / / is now a2
First, through the change of the variable, we can see that object replication using the clone keyword calls the _ _ clone () method. This magic method is at the heart of the prototype pattern. In this method, we can re-instantiate or define reference members in the object. With clone, we re-instantiate the variable so that t becomes a new object, thus avoiding the problems caused by references.
In the replication of objects, we need to pay special attention to the problem of recursive references. That is, the internal reference of the object itself will cause repeated references back and forth to form a recursive loop.
/ / Circular reference problem
Class B
{
Public $that
Function _ clone ()
{
/ / Segmentation fault: 11
$this- > that = clone $this- > that
/ / $this- > that = unserialize (serialize ($this- > that))
/ / object (B) # 6 (1) {
/ / ["that"] = >
/ / object (B) # 7 (1) {
/ / ["that"] = >
/ / object (B) # 8 (1) {
/ / ["that"] = >
/ / * RECURSION* infinite recursion
/ /}
/ /}
/ /}
}
}
$b1 = new B ()
$b2 = new B ()
$b1-> that = $b2
$b2-> that = $b1
$b3 = clone $b1
Var_dump ($b3)
The that in class B points to its own instance, and the two objects point to each other and then replicate, and this endless loop occurs. After using serialization and deserialization of the output, we will see a reference prompt for RECURSION. This is the endless cycle of recursion. This situation must be avoided as much as possible.
In the above example, we use serialization and deserialization to solve the reference problem. In terms of object variables copied by the object (there are more levels of reference variables in the object variables), this approach can solve the reference problem in the top-most object _ _ clone () method at once.
At this point, I believe that everyone on the "PHP object replication example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 299
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.