NotionCommotion Posted February 18, 2017 Share Posted February 18, 2017 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); } //... } } Quote Link to comment Share on other sites More sharing options...
requinix Posted February 18, 2017 Share Posted February 18, 2017 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)). Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted February 18, 2017 Author Share Posted February 18, 2017 Thanks requinix, I guess unsetting $this doesn't make much sense, and I can do whatever necessary in the close() callback. Quote Link to comment Share on other sites More sharing options...
benanamen Posted February 18, 2017 Share Posted February 18, 2017 (edited) Isn't this when you would use a destructor in the class? Edited February 18, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
requinix Posted February 18, 2017 Share Posted February 18, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.