Altec Posted December 10, 2008 Share Posted December 10, 2008 I'm just not getting in to OOP (I understand the basic concept), and just learned about the __destruct() function. I'm a little bit stuck as to using it appropriately. For instance, if I define a public variable: class Car { public $car; } And nothing happens to $car, wouldn't my destruct function look like this: function __destruct() { if(empty($this->car)) { unset($car); } } Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 10, 2008 Share Posted December 10, 2008 You're not defining $car as a "public" variable. You're defining it as an attribute of your Car class, that is accessible publicly (i.e. from outside of the class). $x = new Car('Ford'); echo $x->car; The destructor is only called when an instance of the Car object is destroyed (unset, etc), at which point all the attributes for that instance of Car are destroyed anyway because they are attributes of the instance that you're destroying. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted December 10, 2008 Share Posted December 10, 2008 PHP is pretty smart when it comes to managing memory. You don't need to unset individual variables. Destructors are used to do any absolutely-last-minute work in a class. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 10, 2008 Share Posted December 10, 2008 When just starting with OOP don't bother too much about __destruct(). You may need to redefine it for example, if your object can instantiate other objects during its lifetime. __destruct should then take care of destroing those "child" objects before destroying "main" object. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.