0

Can function __construct() destruct itself after construction?

Like in following pseudo-code, and if yes than how to do it:

function __construct() {
  if($something) echo "OK!"
  else __destruct();

}

9
  • 2
    -1 Have you tried it? Commented Sep 1, 2013 at 18:31
  • Can it destruct itself? Commented Sep 1, 2013 at 18:32
  • 1
    1) have you tried it? 2) why would you even need to manually call __destruct()? 3) WHY!?!?! 4) Wut? Why? Commented Sep 1, 2013 at 18:32
  • To free memory. Yeah I tried Fatal error: Call to undefined function __destruct() in... Commented Sep 1, 2013 at 18:34
  • 1
    Jusr throw up in that case @ulkas Commented Jul 14, 2014 at 8:08

4 Answers 4

4

Method __destruct is a magic method in PHP and must not be called manually.

Hovewer it does not do any real magic, the name of the method should be something like onDestructed or so, just like an event listener.

So, __destruct is called when there are no more references to the object. The implementation of method could be the following:

function __destruct(){
  echo "An object of class " . __CLASS__ . " has been destroyed " ;
}

And to answer your question, yes, it can be called explicitly, and you will just execute the code inside method __destruct, but you will not destroy the object (unless you do some real garbage collection inside it).

function __construct($something) {
  if($something) 
    echo "OK!" ;
  else 
    $this->__destruct(); // $this-> must be used here!
}

To destroy it, you can use unset($object) or just set it to null in some cases.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Does having no reference to object can be done with unset? Are they other ways of removing object? Im afraid that my poor OOP skills will suffocate memory so that am just in case removing all objects I dont need later.
0

If you create an instance of our Object with $test = new YourObject(), it would not be possible anymore to return an instance of it into $test, when you destruct it right away...

Comments

0

You could always use the condition to determine whether the object gets constructed:

   if($something) {
   $object = new Class();
   }

Comments

-1

If you destroy the object in constructor then u can achive your require ment like below.

class class1
{
    function __construct()
    {
        print "constructor\n";
        print "Now destructor going to call\n";
        unset($this);
    }
    function __destruct()
    {
        print "this is destructor\n";
    }
}
$obj=new class1;

In the above example . While creating object it will call the constructor then constructor function destroy itself by unset function then destructor function called and object also destroyed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.