In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use the clone keyword and _ _ clone () method in PHP". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In our previous study, there are a lot of keywords, such as $this, $new, and so on. Today we continue to take a look at the clone clone keyword, which is also commonly used in PHP. Before learning the clone keyword, we need to know that the object model in PHP invokes the object through reference, but I need to suggest a copy of the object in order not to affect the copy of the object when the original object is changed.
At this time, if you use the new keyword, you can recreate the object, and then assign the same value to the property, which can also play the role of creating a new copy, but this method is not only troublesome and error-prone, at this time we hope that PHP can have an identical object, and the two are not interfering with each other, that is, we want to clone. Then how should it be realized? Don't panic. Keep looking.
Clone keyword
We want to have a copy that does not interfere with the original object, and we can clone the object through the clone keyword in PHP.
The syntax format of the clone keyword is as follows:
Clone object name = clone original object name
You can think of cloning as copying the memory area of the object, and the assigned object and the original object are independent of each other. And the member methods, properties, and values between them are exactly the same.
Next, let's take a look at the capabilities of clone through a simple example, as follows:
Output result:
What we need to note is that if you use = to assign an object to a variable, you will get a reference to an object, and changing the value of the property through this variable will affect the original object. So you still have to use clone, and changing the property value will not affect the original object.
Through the above example, we already know how to clone an object for our use, but after the object is cloned successfully, its properties, member methods and values are exactly the same as the original object. at this time, we just say that changing the property will not affect the original object, but how to reassign the cloned replica member property? At this point, we will use the _ _ clone () method in the PHP magic method.
_ _ clone () method
The _ _ clone () method in PHP cannot be called directly. Only when there is clone, there is _ _ clone (). Only when an object is cloned by the clone keyword can the _ _ clone () method be called through the cloned object.
At this point, when you use clone to create a copy, you need to note that PHP checks to see if _ _ clone () exists.
If it does not exist, PHP assigns all the properties of the object by default; if _ _ clone () exists, that is, it is defined, then it is responsible for the properties of the new object.
In addition, let's take a look at an example, which is as follows:
Output result:
From the above example, you can see that the _ _ clone () method does not require any parameters. Another thing we can know is that if you want to disable cloning, you only need to set an empty _ _ clone () method with access to private (private) in the class.
That's all for "how to use the clone keyword and the _ _ clone () method in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 0
*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.