tutsplus object oriented tutorial: calling the destruct method

I modified tutsplus object oriented php tutorial
$obj3 called the destruct method but this did not destroy the object
I know that unset will destroy the object, but why can I not just call destruct?

<?php
class MyClass
{
  // class properties and methods go here
  public $prop1="I'm a class property!";
 public function __construct(){
   echo '<br>The class '. __CLASS__.' was inititiated!<br>';
 }
 public function __destruct(){
   echo '<br>The class '.__CLASS__.' was destroyed!<br>'; 
 }
 public function __toString(){
   echo "using the toString method: ";
   return $this->getProperty();
 }
 public function setProperty($newval) {
   $this->prop2=$newval;
 }
 
 public function getProperty(){
   return $this->prop1. "<br>";
 }
  
}

class MyOtherClass extends MyClass
{
  public function __construct()
  {
    parent::__destruct();
    parent::__toString();
    parent::__construct();//call the parent class's constructor
    echo '<br>a new constructor in '.__CLASS__;
    $this->prop3='3 three';
  }
  public function newMethod()
  {
    echo '<br>from a new method in '.__CLASS__.'<br>';
  }
  public function getProperty(){
   return $this->prop1. "<br>";
 }
}
//create two objects
$obj= new MyClass;
$obj2=new MyClass;
$obj3=new MyOtherClass;
//get the value of $prop1 from both objects
echo $obj->getProperty();
echo $obj2->getProperty();
echo $obj3->newMethod();

//set the new values for both objects
$obj->setProperty("I'm a new property value!");
$obj2->setProperty("I belong to the second instance!");


//output both objects' $prop1 value
echo $obj->getProperty();
echo $obj2->getProperty();
echo $obj3->getProperty();
unset($obj);
echo $obj2;
echo '<br><pre>';
print_r($obj);
print_r($obj2);
print_r($obj3);
echo '</pre><br>';

Open in new window


output
The class MyClass was inititiated!

The class MyClass was inititiated!

The class MyClass was destroyed!
using the toString method: 
The class MyClass was inititiated!

a new constructor in MyOtherClassI'm a class property!
I'm a class property!

from a new method in MyOtherClass
I'm a class property!
I'm a class property!
I'm a class property!

The class MyClass was destroyed!
using the toString method: I'm a class property!


MyClass Object
(
    [prop1] => I'm a class property!
    [prop2] => I belong to the second instance!
)
MyOtherClass Object
(
    [prop1] => I'm a class property!
    [prop3] => 3 three
)



The class MyClass was destroyed!

The class MyClass was destroyed!

Open in new window

LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
You can't simply call __destruct method because this method doesn't destroy nothing:

 public function __destruct(){
   echo '<br>The class '.__CLASS__.' was destroyed!<br>';
 }

As you can see, this method only says the class has been destroyed but it doesn't actually unsets it. In the same way, __construct method doesn't execute nothing but saying the object was c reated: the object is created calling the new() method not the __construct one

$obj= new MyClass;     vs     $obj = MyClass->construct; (?!?)

So you must call unset() method: when you call new, the object is created and the __construct method is called internally and it outputs 'The object was created'; when you call unset() the __destruct method is called internally and the output is 'The object was destroyed'.

Hope I've been clear :)
Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Objects are destroyed by unset() or when there are no longer any references to the object, or at shutdown.  When the object is destroyed, if you want to do anything as you say "Goodbye" you put the code you want to run into the __destruct() method.  I don't use it very much.  The only examples I could find were in my demonstration libraries.
http://php.net/manual/en/language.oop5.decon.php
0
rgb192Author Commented:
destruct
is called after unset or end of php and does not destroy

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.