Avatar of rgb192
rgb192
Flag for United States of America asked on

more information about object that was destroyed

problem:
I am learning about destructor method
and there are many ways to destroy object (unset, end of file)
and I echo
'in destructor method'

I look at long output file and do not understand which object destroyed




$obj=new className();
unset($obj);

can we echo anything about $obj in destructor
$obj which was created in __LINE__(line of creation) is destroyed


could we get more information using reflection class


an easy answer would be outside the class

echo '<br>information about object about to be destroyed'
var_dump($obj);
unset($obj);


but this answer does not cover example where object is destroyed at end of file




I am looking to learn instead of a quick fix to problem
PHP

Avatar of undefined
Last Comment
rgb192

8/22/2022 - Mon
Ray Paseur

Here is a code snippet showing what a destructor can do:
<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

class Thing
{
    protected $info;

    public function __construct()
    {
        $this->info = 'Hello World';
    }

    public function __destruct()
    {
        error_log('DESTRUCTOR CALLED IN ' . __CLASS__ . ' AT ' . __LINE__);
    }
}

// CREATE AN INSTANCE OF THE CLASS
$obj = new Thing;

// SHOW THE OBJECT INSTANCE OF THE CLASS
var_dump($obj);

// UNSET THE OBJECT
unset($obj);

Open in new window

First instance of the error log was created by unset()
[01-May-2014 17:21:12 America/Chicago] DESTRUCTOR CALLED IN Thing AT 15

Open in new window

Next, I removed the unset() so that the destructor is called by the script terminator.  Same message (except for the timetamp).
[01-May-2014 17:23:44 America/Chicago] DESTRUCTOR CALLED IN Thing AT 15

Open in new window

Could you get more information from Reflection?  Possibly.  A more reasonable approach would be for you to tell us what you want to know and why.  Then we may be able to help you find the information.
rgb192

ASKER
<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

class Thing
{
    protected $info;

    public function __construct()
    {
        $this->info = 'Hello World';
    }

    public function __destruct()
    {
        //error_log('DESTRUCTOR CALLED IN ' . __CLASS__ . ' AT ' . __LINE__);
        echo'DESTRUCTOR CALLED IN ' . __CLASS__ . ' AT ' . __LINE__;
    }
}

// CREATE AN INSTANCE OF THE CLASS
$obj = new Thing;

// SHOW THE OBJECT INSTANCE OF THE CLASS
var_dump($obj);

// UNSET THE OBJECT
unset($obj);

Open in new window


object(Thing)#1 (1) { ["info":protected]=> string(11) "Hello World" } DESTRUCTOR CALLED IN Thing AT 16

line 16 is the echo (i changed the error_log) which in inside the class

if possible I was looking for something about $obj in the class
where was it created (line number of creation outside the class)
'we are destructing an object named $obj which was created in line 21'

because in my learning I stopped looking at echo statements because they are confusing and using only on ide
Ray Paseur

Sorry - I don't know how to do that.  Maybe Reflection can help, but Reflection seems to be about reverse-engineering, so I have never explored it in my work -- I already know what my code does and do not have to reverse-engineer it.  So the issue has never come up for me.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
rgb192

ASKER
    public function __get($var)
    {
        if (isset($this->$var))
        {
            echo '<h1>MAGIC METHOD __GET() CALLED FOR ' . "<i>$var</i>" . ': ' . $this->$var . '</h1>';
        }
        else
        {
            echo '<h1>MAGIC METHOD __GET() CALLED FOR UNDEFINED: ' . "<i>$var</i>" . '</h1>';
        }
    }

Open in new window


from another expert comment in  question about get

but destructor does not take argument
Member_2_248744

Greetings rgb192, , you say - "if possible I was looking for something about $obj in the class"

the Class Definition, is NOT the working memory created "Object" (the $obj in your code), so the "object" and the  "Class Definition" do have a relationship, but in the workings of PHP code they ARE NOT the same thing.

In the "Class Definition" of written code for that Class, if you want to access the current "object" reference of the PHP memory that's running that particular "object", you use the $this  variable.

But the code inside a Class Definition, has no meaning for line numbers and areas of code OUTSIDE the class.

you can have something like -

public function __destruct()
    {
        echo '__destruct for '.__CLASS__.', as object - ';
        var_dump($this);
    }

for a possible "learning" code write, as this would show the class Name and the property values in that Object (not the class).
However, the real reason that a  __destruct() method in available, is - - so IF there are "Open" references (database, connections) or "large memory Arrays", that should be CLOSED or released, then put them in the __destruct()

public function __destruct()
    {
    $this->mysqli->close();
    unset($this->large_memory_array);
    }

= = = = =
the use of the refection class, can give you some information, about sections and pieces of the "Class" and or current "Object", here is a list of the infos you can get -
http://php.net/manual/en/book.reflection.php

look at all of the methods under  "ReflectionClass — The ReflectionClass class"  such as -
ReflectionClass::getShortName — Gets short name

But almost all of these reflection methods will tell you about the "Class Code Definition" and little if any about any object instance. I have found little use for the reflection class, because I always seem to have the written class code to see and get more information, than using some method to get it. I really see no good reason for a Class tutorial for beginners to show any about the new and confusing "ReflectionClass"
rgb192

ASKER
Class Definition, is NOT the working memory created "Object" (the $obj in your code)

In the "Class Definition" of written code for that Class, if you want to access the current "object" reference of the PHP memory that's running that particular "object", you use the $this  variable.

so maybe var_dump($this);
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Member_2_248744

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
rgb192

ASKER
thanks

values in JUST ONE object instance, not the Class.

foreach ($this as $key => $value) { }

$obj->id provides information about destructed object

I think Ray's example was for me. Now I understand the code sample better.