As I said in the beginning, it’s perfectly possible to call the destructor of a determined object without having to end the execution of an application or script. Since in all the cases a destructor will be invoked when its pertinent object no longer exists, this process can be triggered manually simply by removing the object via the “unset()” PHP native function. As you’ll possibly know, this function comes in handy for removing unwanted variables of all types, and these also include objects. The concept is theoretically interesting, therefore I’m going to demonstrate how to manually call a destructor by using the same “User” class that you saw in previous articles of the series. Its respective definition was the following: // 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>'; } } } Now that you hopefully recalled how the above class does its business, let me show you a concrete example where its respective constructor is called manually using the popular “unset()” PHP function. Having said that, here’s how this brand new example looks: try{ // create user $user=new User('John','Doe','john@domain.com'); // display separately user data echo 'First Name: '.$user->getFirstName().'<br />'; // delete object, so this triggers its '__destruct()' method unset($user); echo 'Last Name: '.$user->getLastName().'<br />'; echo 'Email: '.$user1->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user1->getAll();
/* displays the following:
First Name: John
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() Fatal error: Call to a member function getLastName() on a non-object in path/to/example
*/ } catch(Exception $e){ echo $e->getMessage(); exit(); } After studying the previous example, you’ll have to admit that things suddenly become exciting! As you can see, once a generic user handing object is created by the above script, the object in question is deliberately deleted by way of the aforementioned “unset()” PHP function. This trick results in the immediate execution of its destructor, certainly a process shown clearly by the output generated by the prior script. Now, at this point, do you see how easy it is to make a destructor run without waiting for a script to finish? I guess you do! However, the example that you just learned uses only one object along with its corresponding destructor. But what about using multiple objects and destructors that are called manually? Indeed, it sounds like something promising and useful. Thus in the next section, I’m going to show you precisely how to fire up different destructors by using the “unset()” PHP function utilized previously. To learn the details of how this process will be achieved, please click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|