Executing Destructors Manually in PHP 5 - Calling multiple destructors manually (Page 3 of 4 )
During the prior section, you learned how to manually trigger the destructor of a trivial user handling object without having to abruptly stop the execution of a script. As you may have guessed, this approach can be easily extended to work with multiple objects and destructors.
Basically the logic behind this idea remains the same: once these objects are spawned, they’re purposely deleted via the pertinent “unset()” PHP function, as I did earlier.
Having explained how to execute different destructors manually, I’m going to list again the definition of the previous “User” class, so you can recall more easily how it functions:
// define 'User' class
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(){
// display object properties
echo '<h2>Properties of object being destroyed</h2>';
foreach(get_object_vars($this) as $prop=>$val) {
echo '<p>'.$prop.'='.$val.'</p>';
}
// display object methods
echo '<h2>Methods of object being destroyed</h2>';
$methods=get_class_methods(get_class($this));
foreach($methods as $method) {
echo '<p> Method Name: '.$method.'()</p>';
}
}
}
Having listed the signature corresponding to the sample “User” class, now it’s time to demonstrate how to manually trigger the respective destructors of a few user handling objects when they’re removed by way of the “unset()” PHP function.
This process is performed neatly by the hands-on example below, thus I recommend that you have a close look at it:
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();
// delete object, this triggers its '__destruct()' method
echo '<h2>Calling destructor method of first user object!</h2>';
unset($user1);
// 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();
// delete object, this triggers its '__destruct()' method
echo '<h2>Calling destructor method of second user object!</h2>';
unset($user2);
// 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();
/* displays the following
First Name: John
Last Name: Doe
Email: john@domain.com
Complete user information: First Name: John Last Name: Doe Email Address: john@domain.com
Calling destructor method of first user object!
Properties of object being destroyed
firstName=John
lastName=Doe
email=john@domain.com
Methods of object being destroyed
Method Name: __construct()
Method Name: getFirstName()
Method Name: getLastName()
Method Name: getEmail()
Method Name: getAll()
Method Name: __destruct()
First Name: Mary
Last Name: Smith
Email: mary@domain.com
Complete user information: First Name: Mary Last Name: Smith Email Address: mary@domain.com
Calling destructor method of second user object!
Properties of object being destroyed
firstName=Mary
lastName=Smith
email=mary@domain.com
Methods of object being destroyed
Method Name: __construct()
Method Name: getFirstName()
Method Name: getLastName()
Method Name: getEmail()
Method Name: getAll()
Method Name: __destruct()
First Name: Susan
Last Name: Norton
Email: susan@domain.com
Complete user information: First Name: Susan Last Name: Norton Email Address: susan@domain.com
Properties of object being destroyed
firstName=Susan
lastName=Norton
email=susan@domain.com
Methods of object being destroyed
Method Name: __construct()
Method Name: getFirstName()
Method Name: getLastName()
Method Name: getEmail()
Method Name: getAll()
Method Name: __destruct()
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
That wasn’t rocket science, was it? As you can see by the above example, three different user handling objects are first spawned, and then deleted with the “unset()” PHP function. Obviously, the consequences of doing this are that the respective constructors are called according to the order in which these objects are removed.
Quite possibly at this very moment you’re wondering if it’s really useful to trigger destructors manually. Well, in certain conditions you might want to trigger these methods “behind the scenes” without having to terminate the execution of a specific PHP script. However, this depends completely on the application you’re working with and it’s up to you to decide when (if ever) to use this approach.
So far, so good. At this stage, I’m reasonably sure that you learned an alternative approach to call destructors manually. Nonetheless, it’s fair to mention that all the code samples that I built worked with PHP 5. So, there’s a question that remains unanswered: is it possible to emulate the behavior of destructors in PHP 4? Of course it is! And the last section will be entirely focused on discussing this process in detail.
Go ahead and read the next few lines. We’re almost finished!
Next: Emulating destructors in PHP 4 >>
More PHP Articles
More By Alejandro Gervasio