Frage


I'm new at OOP programming. So I have 2 Questions about the __destruct function.

  1. Can I call another object-function from __destruct or are the other functions already undeclarated? For example:

    function __destruct()
        $this->save();
    
  2. Can I also call the __destruct function in my normal code? For example:

    $object_name->__destruct();
    
War es hilfreich?

Lösung

  1. Yes, that would work I guess, although invoking a save() call inside a garbage collector would be kind of counterintuitive (and terrible design.)

  2. Yes, if you want, or it will be invoked automagically by PHP when no more references to the object exist.

Andere Tipps

First of all __destruct is a destructor of the object same as __construct

Both of above functions calls automatically when you create Object (__construct will call) and you delete or destroy the object (__destruct will be called)

You can call N-number of functions from either constructor or from destructor. Main purpose of constructor is to initialize the object and allocate appropriate memory location.

where in case reverse with destructor to deallocate memory as well as any allocated/locked resources such as file/printer or may be any other resource.

You can also invoke any other object's Public methods by using object_name->method name.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top