Analysis of PHP 5 clone function application

Source: Internet
Author: User

Undoubtedly, the launch of PHP5 has a profound significance for the development of network applications. This is not just because it is highly backward compatible with PHP4.

Of course, PHP5 data packets have powerful object models and a set of new functions and libraries, not to mention its portable exception mechanism, which gives it the ability to effectively handle errors and abnormal events. Write files to the server using PHP

Of course, when we use the word "function, we are talking about not only the concept of executing a specified task (for example, reading data from a file or applying a filter for the supplied variable. We also refer to some magical functions (such as PHP 5 clone functions ), that is, the functions that do not have the default task execution and can be automatically called by the PHP engine to respond to certain events.

When the development object points to a PHP application, the _ set (), _ get (), and _ call () methods are typical examples of magic functions, however, there are other magic functions worthy of in-depth analysis by developers. In this article, we will focus on the "_ clone ()" method. As the name suggests, this method can be called from the background when the "clone" PHP keyword is used.

Trigger functions from the background when copying objects

As we mentioned earlier, when using the clone keyword, The _ clone () function (clone function) will be automatically quoted. For the sake of conciseness, we will only talk about the clone keywords used to create the specified object's independent copy, rather than creating references. Use PHP to add HTML to multiple files

Now, the _ clone function is returned, which can give it a clear task indication. To achieve this, let's look at the following code:

 
 
  1.  class User   
  2. {  
  3.  
  4. // constructor (not implemented)  
  5.  
  6. public function _construct(){}  
  7.  
  8. // set undeclared property in a restrictive way  
  9.  
  10. public function __set($property, $value)  
  11.  
  12. {  
  13.  
  14. if (in_array($property, array('fname', 'lname', 'email')) === TRUE)  
  15.  
  16. {  
  17.  
  18. $this->$property = $value;  
  19.  
  20. }  
  21.  
  22. }  
  23.  
  24. // get undeclared property  
  25.  
  26. public function __get($property)  
  27.  
  28. {  
  29.  
  30. if (isset($this->$property))  
  31.  
  32. {  
  33.  
  34. return $this->$property;  
  35.  
  36. }  
  37.  
  38. }  
  39.  
  40. // single point to fetch user data  
  41.  
  42. public function __call($method, $args)  
  43.  
  44. {  
  45.  
  46. if ($method === 'fetch' AND emptyempty($args) === FALSE)  
  47.  
  48. {  
  49.  
  50. return $this->$args[0];  
  51.  
  52. }  
  53.  
  54. }  
  55.  
  56. // implement __clone( method  
  57.  
  58. public function __clone()  
  59.  
  60. {  
  61.  
  62. echo 'Cloning user object.';  
  63.  
  64. }  
  65.  

In this specific example, we want to give the _ clone method a conspicuous task to help you better understand it, but believe that the function can be used to execute more complex tasks. No matter how simple the _ clone function is demonstrated in this article, when copying a simple User-class instance, this function is very helpful to show us how the PHP engine calls the function.

Call the Clone method when copying an object

To understand how PHP explains how a program calls the _ clone () function, the best way is to observe a specific example that demonstrates how to copy a simple User-class instance.

We have created a small script that uses the clone keyword in the clone function to copy a user object and trigger the call to the _ clone () method. The script is defined as follows:

 
 
  1.   $user = new User();   
  2. $user->fname = 'Alejandro';  
  3.  
  4. $user->lname = 'Gervasio';  
  5.  
  6. $user->email = 'alejandro@mydomain.com';  
  7.  
  8. // display user data  
  9.  
  10. echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') .
    ' Email : ' . $user->fetch('email');  
  11.  
  12. /*  
  13.  
  14. displays the following  
  15.  
  16. First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com  
  17.  
  18. */ 
  19.  
  20. // clone user object  
  21.  
  22. $newuser = clone $user;  
  23.  
  24. /*  
  25.  
  26. displays the following  
  27.  
  28. Cloning user object.  
  29.  
  30. */ 

This code is easy to write and understand. As you can see, once the script generates an object in the User class and creates some unstated attributes, it will copy this object. This process automatically calls the related _ clone () method.

This specific method supports more complex and useful task execution. Therefore, if the magic functions attached to PHP5 have caught your attention, you may want to improve your programming skills by using these functions. Such an attempt will be enlightening.

Conclusion

This article describes how to implement and use the _ clone () function. When an object is copied using the clone keyword, this function is automatically referenced. This is the power of the PHP 5 clone function.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.