Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Stoimen Popov:
PHP: Don’t Call the Destructor Explicitly
Jun 27, 2016 @ 17:52:39

In a new post to his site Stoimen Popov makes the recommendation to not call the destructor explicitly in your code and provides some alternatives.

PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++” says the documentation for destructors. [...] Well, as you can not call the constructor explicitly [...] so we should not call the destructor explicitly. The problem is that I’ve seen this many times, but it’s a pity that this won’t destroy the object and it is still a valid PHP code.

He talks about __destruct and it's role in PHP's set of "magic methods" and what they exist to do. He then gets into a few examples of what code could look like that uses a destructor and the difference between normal handling calling the destructor explicitly. The main differences is that calling it explicitly does not destroy the object, it's basically like calling any other method. He does include an interesting method for destroying the object - setting it to null - and notes that the destructor fires then too. He also points out a few interesting things about cloning objects and how object references work when setting nulls as in the previous example.

tagged: destructor explicit call object destroy null tutorial

Link: http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/

Stoimen Popov's Blog:
PHP: Don’t Call the Destructor Explicitly
Nov 16, 2011 @ 17:56:43

In this new post to his blog Stoimen Popov talks about calling the "destructor" method of an object and why doing it directly could lead to some issues - like not actually destroying the object before the script ends.

At the end of the script the interpreter frees the memory. Actually every object has a built-in destructor, just like it has built-in constructor. So even we don’t define it explicitly, the object has its destructor. Usually this destructor is executed at the end of the script, or whenever the object isn’t needed anymore. This can happen, for instance, at the end of a function body. Now if we call the destructor explicitly, which as I said I’ve seen many times, here’s what happen. As you can see calling the destructor explicitly doesn’t destroy the object. So the question is...how to destroy an object before the script stops?

He points out that one way to "destroy" an object is to null it out and remove the structure from memory. This is tricky, though, because a clone of the object will still exist in memory, just not the original.

tagged: destructor call directly null clone object

Link:

DevShed:
Executing Destructors Manually in PHP 5
Feb 13, 2008 @ 17:27:00

Finishing off their series on destructors in PHP5 applications today, DevShed has posted this new tutorial showing how to manually run the destructors of your class (in case there's ever a need).

In this final tutorial of the series I'm going to show you how to manually trigger a destructor, and in addition you'll learn how to emulate the behavior of this magic method when using PHP 4.

They not only talk about how to call the destructor manually but they also show how to call multiple destructors at the same time to destroy lots of objects at once. And, as promised, they include a method for being able to use a destructor-like bit of functionality in a PHP4 app as well.

tagged: php5 destructor execute manual php4 emulate multiple

Link:

DevShed:
Retrieving Information on Several Objects with Destructors in PHP 5
Feb 07, 2008 @ 16:28:00

DevShed has posted the next-to-last (part four) of their series looking at destructors in PHP5 applications today. This time they focus on passing information back out of the destructors as the script is finishing.

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).

They start simple, showing how to handle one object to give you a base to work from. They modify this to expand it out and show the management instead of three different user data objects.

tagged: php5 tutorial destructor manage multiple object data

Link:

DevShed:
Keeping Track of Objects when Using Destructors in PHP 5
Jan 30, 2008 @ 17:19:00

Devshed continues their series looking at the use of destructors in PHP5 applications with part three, a method for keeping track of objects you've created during execution.

In this third part of the series, I'm going to show you how to retrieve some useful information about a specific object, including its properties and methods, prior to its being destroyed by the PHP parser via the implementation of a simple destructor.

Their new user class extracts the details about each of the objects right before they're destroyed via a call to get_object_vars and a loop to display the property and its value.

tagged: php5 destructor object track property

Link:

DevShed:
Implementing Destructors with Multiple Objects in PHP 5
Jan 23, 2008 @ 20:57:00

DevShed has posted part two in their series looking at the use of destructors in PHP5. This time they focus on their use with multiple objects inside of an application.

Destructors can be used in all sorts of clever ways in the context of a given PHP 5 application, mostly in those cases where it's necessary to keep track of the status of certain objects prior to their being destroyed by the PHP interpreter. In this second chapter you'll learn how to work simultaneously with multiple classes that concretely implement their respective destructors.

They revisit their previous example with a simple class that's then extended to allow the flexibility to call the same destructor on multiple instances.

tagged: destructor class user data multiple tutorial

Link:

DevShed:
Understanding Destructors in PHP 5
Jan 17, 2008 @ 15:31:00

DevShed has started up yet another PHP-related series today with this new tutorial covering part of PHP5's object functionality - destructors.

This article, the first part of a five-part series, introduces you to destructors and their use with PHP 5 programs. [...] The word "destructor" relates to certain methods or functions of a determined programming language that are responsible for destroying data that, in the context of that particular language, shouldn't exist any longer.

They create a sample class (for storing user information) and show how to integrate a destructor to remove all traces of the user's information from the properties of the class.

tagged: destructor php5 class tutorial example user information destructor php5 class tutorial example user information

Link:

DevShed:
Understanding Destructors in PHP 5
Jan 17, 2008 @ 15:31:00

DevShed has started up yet another PHP-related series today with this new tutorial covering part of PHP5's object functionality - destructors.

This article, the first part of a five-part series, introduces you to destructors and their use with PHP 5 programs. [...] The word "destructor" relates to certain methods or functions of a determined programming language that are responsible for destroying data that, in the context of that particular language, shouldn't exist any longer.

They create a sample class (for storing user information) and show how to integrate a destructor to remove all traces of the user's information from the properties of the class.

tagged: destructor php5 class tutorial example user information destructor php5 class tutorial example user information

Link:

Alex Netkachov's Blog:
Optimize PHP memory usage: eliminate circular references
Sep 18, 2007 @ 22:17:21

On his blog today, Alex Netkachov has posted a suggestion of how to combat a common issue facing PHP scripts - their memory usage.

PHP has a build-in garbage collector so you do not need to track the links on the objects, allocate memory for objects and delete them when they are not longer necessary. Things seem so perfect that developers do not even know that their scripts allocate a lot of memory until their server stops processing requests because of the out of memory error.

His example is the processing of a parent/child relationship tree. If not managed correctly, the variables being used could get out of hand quite quickly. Alex recommends the use of a "destructor" at the end of each iteration to help destroy variables and values no longer needed.

tagged: optimize memory circular reference destructor variable optimize memory circular reference destructor variable

Link:

Alex Netkachov's Blog:
Optimize PHP memory usage: eliminate circular references
Sep 18, 2007 @ 22:17:21

On his blog today, Alex Netkachov has posted a suggestion of how to combat a common issue facing PHP scripts - their memory usage.

PHP has a build-in garbage collector so you do not need to track the links on the objects, allocate memory for objects and delete them when they are not longer necessary. Things seem so perfect that developers do not even know that their scripts allocate a lot of memory until their server stops processing requests because of the out of memory error.

His example is the processing of a parent/child relationship tree. If not managed correctly, the variables being used could get out of hand quite quickly. Alex recommends the use of a "destructor" at the end of each iteration to help destroy variables and values no longer needed.

tagged: optimize memory circular reference destructor variable optimize memory circular reference destructor variable

Link:


Trending Topics: