There's the possibility that by the time you read the introduction of this article, using multiple destructors in the same PHP 5 application will be a well-known process for you, since this particular situation was analyzed in detail in the previous article of the series. However, if you didn't have the chance to go through that specific tutorial, you might want to take a close look at the following PHP 5 script, which precisely recreates a hypothetical scenario in which several user handling objects are first spawned from a sample "User" class, and then destroyed when the script in question finishes its execution. Although it's obvious, the most relevant thing to highlight here is that all these objects implement a destructor concretely, which is then called in the proper sequence by the PHP 5 interpreter prior to being removed from the web server's memory. That being said, here's the signature of the aforementioned "User" class, so examine it closely: // define 'User' class (it provides a slightly more complete implementation of its destructor) class User{ private $firstName; private $lastName; private $email; public function __construct($firstName,$lastName,$email){ if(!$firstName||strlen($firstName)>32){ throw new Exception('Invalid First Name parameter!'); } if(!$lastName||strlen($lastName)>32){ throw new Exception('Invalid Last Name parameter!'); } if(!$email||!preg_match("/^.+@.+..+$/",$email)){ throw new Exception('Invalid Email parameter!'); } $this->firstName=$firstName; $this->lastName=$lastName; $this->email=$email; } // get user's first name public function getFirstName(){ return $this->firstName; } // get user's last name public function getLastName(){ return $this->lastName; } // get user's email public function getEmail(){ return $this->email; } // get all user data public function getAll(){ return 'First Name: '.$this->firstName.' Last Name: '.$this->lastName.' Email Address: '.$this->email; } // implement a __destruct()' method public function __destruct(){ echo '<h2>Warning! Destroying User: '.$this->firstName.' '.$this- } } Undeniably, the definition of the prior "User" class is very straightforward, so I assume that you shouldn't have much trouble understanding how it works. You should pay special attention, though, to the implementation of its destructor method, which as you can see, is capable of outputting the values of some class properties, such as the ones named ($firstName and $lastName), when automatically called by the PHP parser. With such a useful destructor method at your disposal, it's perfectly possible to build a sample script that first creates different user-related objects, and then displays the data of the current object being destroyed. In this way it constructs an effective mechanism that demonstrates the order followed by the PHP engine to call up the respective destructors. With this idea in mind, below I coded this simple example, which works with multiple objects and destructors. Here it is: 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(); } // output generated by the previous script 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 That was pretty easy to grasp! In this case, all the destructors corresponding to the previous user handling objects will be invoked in the same order utilized for creating them. Certainly this is a procedure demonstrated by the above script's output. So far, everything seems to be fine, because you've hopefully learned how to work with several objects that concretely implement their respective destructors. However, now that you've digested all this useful material, it's more than probable that you might have asked yourself the following question: is it possible to implement a destructor that can keep track of the properties and methods of a determined object prior to its merciless destruction? (That was pretty dramatic...) Well, the answer is an emphatic yes! And bearing this in mind, in the next section I'm going to show you how to modify the destructor of the same "User" class defined previously so it can provide detailed information about a particular object right before that object is destroyed by the PHP engine. To see how this will be done, please click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|