In accordance with the concepts that I deployed in the section you just read, I'm going to modify the destructor of the sample "User" class that you learned previously so that it can display some useful information, such as methods and properties, about an object that is about to be destroyed. To achieve this process as painlessly as possible, below I included the improved definition of the "User" class, which this time implements its destructor in a more useful way. Have a close look at its definition, please: // 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>'; } } } As you can see, things are getting really exciting now, since the above user handling class offers a more insightful implementation for its destructor. In this specific case, the destructor in question uses some well-known PHP native functions, like "get_object_vars()" and "get_class_methods()," in order to display some useful information about a particular object prior to its deletion. Of course, there is plenty of room to experiment and have fun here by implementing the destructor in different ways, according to your specific needs. But this hands-on example should give you an approximate idea of how to define a destructor that eventually provides relevant data on a specific object. So far, so good. At this point I've already taught you how to implement a destructor and provide it with the ability to display the values of the methods and properties assigned to a specific object, right before its destruction. However, if you're as curious as me, then surely you'll want to see how this method works in the context of a practical example. Therefore, in order to learn how this example will be constructed, please visit the following section. It's only one click away.
blog comments powered by Disqus |
|
|
|
|
|
|
|