Home arrow PHP arrow Page 3 - Retrieving Information on Several Objects with Destructors in PHP 5

Working with different user handling objects - PHP

Welcome to the fourth part of the series “Understanding Destructors in PHP 5.” Made up of five comprehensive tutorials, this series brings to you the foundations of how to declare and implement destructors in PHP 5-driven classes. And it tackles this process from a practical point of view, since you’ll be able to learn these methods by using copious examples.

TABLE OF CONTENTS:
  1. Retrieving Information on Several Objects with Destructors in PHP 5
  2. Keeping track of a single object
  3. Working with different user handling objects
  4. Displaying data related to different objects using one single destructor
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
February 06, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: