Jump to content

Close and unset $this


NotionCommotion

Recommended Posts

Is it possible to close a connection and delete an object when within that object?  For instance:

<?php
//....
$socket->on('connection', function (\React\Socket\ConnectionInterface $stream){
    $client = new DuplexStreamInterface($stream);
    //....    
});
//....

class LengthPrefixStream
{
    public function __construct(DuplexStreamInterface $socket){
        $this->socket = $socket;
        $this->socket->on('data', function($data){
            $this->buffer .= $data;
            $this->parseBuffer();
        });
    }

    public function parseBuffer(){
        //...
        if($badConnectionSoCloseSelf) {
            $this->socket->close();
            unset($this);
        }
        //...
    }
}
Link to comment
Share on other sites

You can call $this->close() or whatever to "close a connection".

 

You cannot unset $this. I can't think of any reason why you should need to. If the calling code wants to destroy the object then it needs to do that (and by "destroy" I mean remove any reference it has and forget about the whole ordeal (however the object may be referenced somewhere else)).

Link to comment
Share on other sites

Isn't this when you would use a destructor in the class?

If you mean using it for closing connections and stuff that the class needs, yes. You can't tell PHP to destroy an object - just unset variables/let them drop out of scope and rely on PHP to clean up as it goes.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.