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:
- class User
- {
-
- // constructor (not implemented)
-
- public function _construct(){}
-
- // set undeclared property in a restrictive way
-
- public function __set($property, $value)
-
- {
-
- if (in_array($property, array('fname', 'lname', 'email')) === TRUE)
-
- {
-
- $this->$property = $value;
-
- }
-
- }
-
- // get undeclared property
-
- public function __get($property)
-
- {
-
- if (isset($this->$property))
-
- {
-
- return $this->$property;
-
- }
-
- }
-
- // single point to fetch user data
-
- public function __call($method, $args)
-
- {
-
- if ($method === 'fetch' AND emptyempty($args) === FALSE)
-
- {
-
- return $this->$args[0];
-
- }
-
- }
-
- // implement __clone( method
-
- public function __clone()
-
- {
-
- echo 'Cloning user object.';
-
- }
-
- }
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:
- $user = new User();
- $user->fname = 'Alejandro';
-
- $user->lname = 'Gervasio';
-
- $user->email = 'alejandro@mydomain.com';
-
- // display user data
-
- echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') .
' Email : ' . $user->fetch('email');
-
- /*
-
- displays the following
-
- First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com
-
- */
-
- // clone user object
-
- $newuser = clone $user;
-
- /*
-
- displays the following
-
- Cloning user object.
-
- */
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.