Implementing Destructors with Multiple Objects in PHP 5 - Handling user-related data with a simple PHP 5 class (
Page 2 of 4 )
Before I teach you how to work with multiple classes that provide concrete implementation for each of their destructors, it’s convenient to refresh a few fundamental concepts concerning the use of them at a very basic level. To do so, I’m going to use the sample class that I constructed in the last tutorial. As you'll recall, it came in handy for managing common user-related data, including first and last names and email addresses.
Essentially, the signature of this user handling class looked like this:
// basic example on using a '__destruct()' method
// 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(){
echo '<h2>Destroying user now...<h2>';
}
}
Frankly speaking, I must say that the definition of the above “User” class is extremely easy to understand. So you shouldn’t have much of a problem grasping how it functions. However, I’d like you to pay close attention to the “__destruct()” method declared in the end. In this case, as with all of the magic methods provided by PHP 5, it’s preceded by two underscore (__) characters and indicates that the interpreter is going to execute it right before destroying an object created with this class.
This concept may sound rather confusing if it’s not accompanied by a practical example. Therefore, below I coded a simple script that spawns a new user object, displays its properties' values on the browser, and then, as you’d expect, triggers the corresponding destructor prior to finishing its execution.
Having explained that, please take a look at the following code sample:
try{
// create new instance of 'User' class
$user=new User('John','Doe','john@domain.com');
// display separately user data
echo 'First Name: '.$user->getFirstName().'<br />';
echo 'Last Name: '.$user->getLastName().'<br />';
echo 'Email: '.$user->getEmail().'<br />';
// display all user information
echo 'Complete user information: '.$user->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
Destroying user now...
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
There you have it. Check the above example with your own testing web server and you’ll be amazed at how well it works! Perhaps it’s not going to change your life as a PHP programmer forever, but undoubtedly it’s going to show you how a destructor functions. In this case, the implementation of the destructor is rather primitive, yet this example should fire up your creativity so that you use your own class destructors in a more useful way.
Well, at this moment you’ve hopefully recalled the foundations of working with destructors in PHP 5. So assuming that this is true, the next thing that I’m going to teach you will be how to use multiple objects that concretely implement their respective destructors. In doing this, you’ll be able to see very clearly in what order these methods are called by the PHP interpreter right before the objects are destroyed.
To find out how this will be done, please visit the following section and keep reading.