We'll start demonstrating how to use a destructor to keep track of the methods and properties of multiple objects by recreating the hands-on example developed in the preceding article of the series. In that part I showed you how to perform this task, but using only one instance of the sample “User” class. So, with that idea in mind, below I included the signature of this user handling class, along with the definition of a basic script, which outputs to the browser the properties and methods of a generic user object prior to being deleted by the PHP engine. The pertinent code sample is as follows: // 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>'; } } } try{ // create user object $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
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()
*/ } catch(Exception $e){ echo $e->getMessage(); exit(); } After studying in detail the above example, you’ll have to agree with me that destructors can be pretty useful for logging purposes. As you can see, this particular case shows how to use this magic method to print the values assigned to the pertinent properties of a simple user object, in addition to showing the names of its method. This example itself is a no-brainer, but it should give you an approximate idea of how to use a destructor to develop an effective log mechanism that can be triggered before one or more objects no longer exist in the context of a given PHP 5 application. Please focus your attention again on the above code sample, since there’s one interesting thing to stress: as you can see, it demonstrates how to display some useful information about one particular object, but you’re probably wondering if this same process can be performed with multiple objects. Fortunately, the answer is a resounding yes! And not only is it possible to carry out this task, but it’s extremely easy to do. Thus, in the next section, I’m going to teach you how to print, on the browser, data associated with the structure of different objects, including their methods and properties, by using the functionality brought by a single destructor. In order to learn the details of how this process will be performed, please visit the following section and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|