Retrieving Information on Several Objects with Destructors in PHP 5

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.

As you know, a destructor is simply a magic method supported by the highly-improved object model of PHP 5. When implemented by a determined object, it will be automatically called by the interpreter prior to removing the object in question from the web server’s memory.

Obviously, the intrinsic nature of destructors make them very useful for performing certain tasks, right before a specific application finishes its execution and all of its objects are deleted. Therefore, if you wish to learn how to use them as part of your own PHP 5 classes, this group of educational articles might be the guide that you’ve been searching for.

Now that you know what this article series is about, it’s time to review the topics that I discussed in the last article concerning the adequate utilization of destructors with PHP 5. Put in a simple way, during the aforementioned tutorial I showed you how to implement a destructor in a useful fashion. This is because I defined a highly generic user-handling class that included into its API a destructor method that was capable of displaying on the browser the methods and properties of a specific object prior to its corresponding deletion by the PHP interpreter.

Indeed, the use of destructors to keep track of the properties (and eventually the methods) of a determined object can be relatively helpful, especially in those situations where a specific PHP 5 application requires you to trace the status of some of its objects before they are removed by the PHP engine.

And now that I’ve spoken of keeping track of methods and properties of certain objects via their respective destructors, in this fourth article of the series I’m going to teach you how perform this process when working with multiple objects (remember that you already learned how to achieve this using only one class instance).

Hopefully, by the end of this installment you’ll be equipped with the required background to build a simple mechanism that will permit you to keep track of the properties and methods of a bunch of objects by using the functionality provided by their respective destructors.

With the preliminaries out of our way, let’s move forward and continue learning more useful features concerning the usage of destructors with PHP 5. You’ll have a very good time, that’s guaranteed!

{mospagebreak title=Keeping track of a single object}

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.

{mospagebreak title=Working with different user handling objects}

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.

{mospagebreak title=Displaying data related to different objects using one single destructor}

As I promised you in the section that you just read, here’s the output generated by the script developed before. As you’ll see in a moment, it displays not only the first and last names and email addresses of those fictional users, but prints their respective methods and properties as the destructor is being called by the PHP interpreter.

Having explained that, please take a look at the following screen results:


First Name: John

Last Name: Doe

Email: john@domain.com

Complete user information: First Name: John Last Name: Doe Email Address: john@domain.com


First Name: Mary

Last Name: Smith

Email: mary@domain.com

Complete user information: First Name: Mary Last Name: Smith Email Address: mary@domain.com


First Name: Susan

Last Name: Norton

Email: susan@domain.com

Complete user information: First Name: Susan Last Name: Norton Email Address: susan@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()

 

Properties of object being destroyed

firstName=Mary

lastName=Smith

email=mary@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()

 

Properties of object being destroyed

firstName=Susan

lastName=Norton

email=susan@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()


As illustrated above, by using only one simple destructor, it’s perfectly possible to display data related to the structure of some trivial user handling objects prior to the corresponding deletion by the PHP engine. Short and effective!

So far, so good. At this point, you hopefully have a more solid background on using destructors with PHP 5 to collect data concerning the structure of certain objects. In addition, you’re free to use all of the code samples built here, with the purpose of expanding your skills in utilizing these useful methods.

Final thoughts

In this fourth part of the series, you learned, with the help of a few easy-to-follow examples, how to exploit the functionality provided by destructors to retrieve information about a bunch of objects before they were removed from memory.

But in this case, it’s fair to mention that all of the examples shown so far have used destructors that are called automatically by the PHP engine. Therefore, in the final article of the series I’ll teach you how to manually trigger a destructor method before giving this chance to the interpreter.

Intrigued about how this will be done? Don’t miss the last chapter of this journey!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan