In Ruby, I have a DAO class that is extended by a class that simplifies connection management, which is extended by a class that represents and processes data in a database, which is further extended by another class. To use the animal metaphor, it would look like this:
class Animal ... end class Mammal < Animal ... end class Feline < Mammal ... end class Cat < Feline ... end class Lion < Cat ... end ...
In PHP, there is a __destruct method that runs when a class is destroyed / deleted. And if this class extends another class, you simply add parent::__destruct() to the __destruct class __destruct as follows:
public function __destruct() {
I could have a similar method for all classes except Animal . Since it does not expand anything, the line parent::__destruct(); no longer valid.
However, as I understand it, Ruby does not have such a method for its objects. The finalizer can be installed, but I decided to just add a cleanup method, which I can call when I want to destroy / delete the class. This will take care of everything that needed to be done before I set the class to nil .
This creates a new problem. If the method is always called cleanup and I call lion_instance.cleanup , I assume that it calls Lion#cleanup . How then to force it to call cleanup in the Cat class, and then Feline and in the chain?
Or is this the wrong approach and you have an idea?
source share