In consonance with the concepts I deployed in the section you just read, I'm going to code a basic script that will create multiple instances of the “User” class that you saw previously. Then, it will echo on the browser the respective first and last names of these user objects just created. And finally, it will finish its normal execution. However, since each of these objects implements a destructor, they will be called logically by the PHP parser, which will also help you know the order in which the respective destructors are invoked before the objects in question are destroyed. Sounds like a simple yet educational experiment, right? Having said that, here’s the script that works with multiple destructors, so study its source code: try{ // create first user $user1=new User('John','Doe','john@domain.com'); // display separately user data echo 'First Name: '.$user1->getFirstName().'<br />'; echo 'Last Name: '.$user1->getLastName().'<br />'; echo 'Email: '.$user1->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user1->getAll();
// create second user $user2=new User('Mary','Smith','mary@domain.com'); // display separately user data echo 'First Name: '.$user2->getFirstName().'<br />'; echo 'Last Name: '.$user2->getLastName().'<br />'; echo 'Email: '.$user2->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user2->getAll();
// create third user $user3=new User('Susan','Norton','susan@domain.com'); // display separately user data echo 'First Name: '.$user3->getFirstName().'<br />'; echo 'Last Name: '.$user3->getLastName().'<br />'; echo 'Email: '.$user3->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user3->getAll();
} catch(Exception $e){ echo $e->getMessage(); exit(); } As you can see in the above example, three different objects are created using the corresponding “User” class, and then their properties are outputted to the browser. Finally, the program stops its execution. So far, nothing unexpected happens. But if you look into the output generated by the previous script, guess what? Yes, you’re correct! Their pertinent destructors are called up in the same order used to spawn the originating objects. This process is clearly demonstrated by the following listing: First Name: John Last Name: Doe Email: john@domain.com Complete user information: First Name: John Last Name: Doe Email Address: john@domain.com First Name: Mary Last Name: Smith Email: mary@domain.com Complete user information: First Name: Mary Last Name: Smith Email Address: mary@domain.com First Name: Susan Last Name: Norton Email: susan@domain.com Complete user information: First Name: Susan Last Name: Norton Email Address: susan@domain.com
Warning! Destroying User: John Doe Warning! Destroying User: Mary Smith Warning! Destroying User: Susan Norton At this point, I've provided you with an instructive example on how to work with multiple objects that concretely implement their corresponding destructors, a process that came in handy for seeing the order in which they’re called by the PHP interpreter. My final recommendation concerning the use of destructors is that you develop your own test examples based on the concepts that you learn in this article series. You’re going to have fun, trust me! Final thoughts In this second installment of the series, I went a bit deeper into the implementation of destructors with PHP 5 and showed you how to keep track of the order in which these class methods are invoked by the PHP engine when working with multiple objects. In the upcoming article, I’m going to teach you how to define destructors in such a way that they can display useful information about a particular object prior to being destroyed by the PHP interpreter. Want to learn how this will be done? Don’t miss the next part!
blog comments powered by Disqus |
|
|
|
|
|
|
|