PHP5 clone function application analysis _ PHP Tutorial

Source: Internet
Author: User
Application Analysis of PHP5 clone function. 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 are powerful, and 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. // constructor (not implemented)
  4. public function _construct(){}
  5. // set undeclared property in a restrictive way
  6. public function __set($property, $value)
  7. {
  8. if (in_array($property, array('fname', 'lname', 'email')) === TRUE)
  9. {
  10. $this->$property = $value;
  11. }
  12. }
  13. // get undeclared property
  14. public function __get($property)
  15. {
  16. if (isset($this->$property))
  17. {
  18. return $this->$property;
  19. }
  20. }
  21. // single point to fetch user data
  22. public function __call($method, $args)
  23. {
  24. if ($method === 'fetch' AND emptyempty($args) === FALSE)
  25. {
  26. return $this->$args[0];
  27. }
  28. }
  29. // implement __clone( method
  30. public function __clone()
  31. {
  32. echo 'Cloning user object.';
  33. }
  34. }

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. $user->lname = 'Gervasio';
  4. $user->email = 'alejandro@mydomain.com';
  5. // display user data
  6. echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') .
    ' Email : ' . $user->fetch('email');
  7. /*
  8. displays the following
  9. First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com
  10. */
  11. // clone user object
  12. $newuser = clone $user;
  13. /*
  14. displays the following
  15. Cloning user object.
  16. */

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.


Bytes. This is not just because it is highly backward compatible with PHP4. Of course, PHP5 data packets are powerful...

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.