In accordance with the concepts that I expressed in the previous section, I intend to demonstrate how to use the same “User” class that you saw earlier to retrieve information on multiple user objects, instead of only one. First, I’m going to list the signature of the aforementioned “User” class, so you can more easily recall how it looks. Here it is: // 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>'; } } } Since the above user handling class was discussed previously, I’m not going to waste your valuable time explaining how it works. Instead, I’ll do something slightly more useful and show you how to use this class to create three brand new user objects, and then display their methods and properties with the assistance of their respective destructors. To perform this interesting task successfully, I’m going to use the following PHP 5 script: try{ // create first user $user1=new User('John','Doe','john@domain.com'); // display separately user data echo 'First Name: '.$user1->getFirstName().'<br />'; echo 'Last Name: '.$user1->getLastName().'<br />'; echo 'Email: '.$user1->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user1->getAll();
// create second user $user2=new User('Mary','Smith','mary@domain.com'); // display separately user data echo 'First Name: '.$user2->getFirstName().'<br />'; echo 'Last Name: '.$user2->getLastName().'<br />'; echo 'Email: '.$user2->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user2->getAll();
// create third user $user3=new User('Susan','Norton','susan@domain.com'); // display separately user data echo 'First Name: '.$user3->getFirstName().'<br />'; echo 'Last Name: '.$user3->getLastName().'<br />'; echo 'Email: '.$user3->getEmail().'<br />'; // display all user information echo 'Complete user information: '.$user3->getAll(); } catch(Exception $e){ echo $e->getMessage(); exit(); } As you can see, the above script simply creates three objects that store information about some fictional users, and then, by way of the pertinent methods, this data is printed straight to the browser. Nothing to complex to grasp, right? Nevertheless, as you may guess, there’s something else happening behind the scenes, since when the script finishes running, obviously it calls each of the destructors of the objects in question. That is a very interesting process. In this case, only one class method is utilized to retrieve data about multiple objects, which demonstrates in a nutshell how useful a destructor can be when adequately implemented. However, I do want you to digest all of this material in small bits, so in the last section of this tutorial I’m going to show you the output produced by the prior script. Thus I suggest you take a deep breath and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|