Home arrow PHP arrow Using the Clone Magic Function in PHP 5

Using the Clone Magic Function in PHP 5

If you’re an eager PHP developer who wants to have at your disposal a quick guide that shows you how to work with the most relevant magic functions provided by PHP 5, then this series of articles might be what you’re looking for. In this fifth part of a seven-part tutorial on magic functions, we'll briefly review the sleep and wakeup functions, and then tackle the clone function.

  1. Using the Clone Magic Function in PHP 5
  2. Review: the sleep and wakeup magic functions
  3. Triggering functions in the background when cloning objects
  4. Calling the clone method when cloning an object
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
June 15, 2009

print this article



Undoubtedly, the release of PHP 5 quite some time ago brought with it a significant advance in the way that web applications are developed nowadays. This is despite the high level of backward compatibility it maintains with its venerable predecessor, PHP 4.

Certainly it’s not breaking news that PHP 5 comes packaged with a robust object model and a whole new set of functions and time-saving libraries, not to mention its handy exception mechanism, which allows it to handle errors and exceptional events in a truly efficient and elegant way.

Of course, when I use the term “functions,” I’m not talking about only the common ones that perform specific tasks such as reading data from a file or applying a filter to a supplied variable. I'm also referring to the magic ones; that is, the functions that have no a concrete implementation by default and are called automatically by the PHP engine in response to certain events.

The trio comprised of the “__set()”, “__get()” and “__call()” methods offer a classic example of magic functions that are used when developing object-oriented PHP applications, but there are a few others that certainly deserve deeper analysis as well.

And speaking of magic functions, you’ll surely recall that I left off the last article discussing the use of the “__sleep()” and “__wakeup()” methods, which are invoked by the PHP interpreter when serializing and unserializing a specified object.

Though these methods were only responsible for echoing to the screen some basic messages when called, the examples developed in the aforementioned tutorial should give you a clear idea of how to use them in a more helpful manner.

Though, as I said before, there are other magic functions that are worth exploring in detail as well. So, with that goal in mind, in this fifth part of the series I’m going to take a closer look at the “__clone()” method, which as its name suggests, is called behind the scenes when using the “clone” PHP keyword.

So, now it’s time to leave the theory behind us and start learning now to use this brand new magic function. Let’s get started right away!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: