in reply to Re: Inheritance and objects tied to a database
in thread Inheritance and objects tied to a database

I agree. It does look cleaner, but how would that help?
  • Comment on Re: Re: Inheritance and objects tied to a database

Replies are listed 'Best First'.
Re: Re: Re: Inheritance and objects tied to a database
by gjb (Vicar) on Dec 20, 2002 at 13:00 UTC

    Since the child is initialized with the attribute values of the parents, it "inherits" a reference to the tied variable, so parent and child share the same data. Untieing one is untieing the other.

    Hope this helps, -gjb-

      So that would involve untieing the $self->{data} in the child class, but NOT untieing it in the parent class? That's the only way I imagine it would work, but I don't know how to do that (how to prevent calling the parent's DESTROY() method. If both parent's and child's DESTROY() methods are called, the parent's DESTROY() method still overwrited the tied data with its copy as opposed to updated child's data.

      Maybe I'm just trying to do something that doesn't really have to be done. I just want to know if I'm going all wrong about the whole thing.

        Since $self->{data} is just an attribute, you can set it to undef after the untie in the child method, and adding an if (defined $self->{data}) {...} around the untie in the parent's DESTROY.

        Hope this helps, -gjb-

        I reckon you only need to untie and destroy the object once - if the data's shared then updating the child's data updates the parent's data automatically.

        By the sounds of it, you don't need to worry about DESTROY in the child - just call the parent module's DESTROY and let it take care of the untieing of the object and the DB update.

        Hope that helps ..
        -- Foxcub