Since my purpose here is to demonstrate how multiple destructors are called by the PHP parser, when working with several objects I’m going to modify the signature of the previous “User” class and more specifically, its destructor method. Basically, the method in question will now be capable of indicating what object is being used before the PHP interpreter removes it from memory (destroyed, in crude terms). Now that you know the scenario, please take a look at the modified definition of the sample user class, which is as follows: // 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->lastName.'<h2>'; } } Now, the destructor method of the above “User” class has been provided with the capacity for displaying on the browser the first and last name properties of the object that is about to be destroyed. This is convenient for finding out the order in which destructors are called when using multiple objects. At this stage, having improved the implementation of the destructor that belongs to the previous user class, it’s appropriate for you to read the next section of the article. In it you’ll learn how to keep track of the order used by the PHP parser to call several destructors when using different user objects. This juicy topic will be discussed in detail in the next few lines, so go ahead and read them please.
blog comments powered by Disqus |
|
|
|
|
|
|
|