NotionCommotion Posted May 21, 2019 Share Posted May 21, 2019 (edited) splobjectstorage.attach shows how to add an object inside the storage and associate it to some data. How should one replace the data associated to an object already in storage? All of the following solutions seem to work, however, I would like confirmation as I couldn't find and specific direction in the documentation. Assuming that detaching shown in my options 3 and 4 is not required, right? Probably option 2? Or, would it be best not to replace the data in storage, but add a shell object to the storage and update the data property? Use case is the object is a stream and the data is a specific type of client which can change over time. <?php class dataA{} class dataB{} class dataC{} class dataD{} class dataE{} class obj{} $storage = new SplObjectStorage(); $obj=new obj; $storage->attach($obj, new dataA); $data = $storage[$obj]; var_dump($data); //Option 1 $storage->attach($obj, new dataB); $data = $storage[$obj]; var_dump($data); //Option 2 $storage->offsetSet($obj, new dataC); $data = $storage[$obj]; var_dump($data); //Option 3 $storage->detach($obj); $storage->attach($obj, new dataD); $data = $storage[$obj]; var_dump($data); //Option 4 $storage->detach($obj); $storage->offsetSet($obj, new dataE); $data = $storage[$obj]; var_dump($data); object(dataA)#3 (0) { } object(dataB)#4 (0) { } object(dataC)#3 (0) { } object(dataD)#4 (0) { } object(dataE)#3 (0) { } Edited May 21, 2019 by NotionCommotion Added thought about not changing Quote Link to comment Share on other sites More sharing options...
kicken Posted May 21, 2019 Share Posted May 21, 2019 You can just re-attach it to change the data, or just use it as an array. //Option 1 $storage->attach($obj, new dataB); $data = $storage[$obj]; var_dump($data); //Option 2 $storage[$obj]=new dataC; $data = $storage[$obj]; var_dump($data); offsetSet is there because SplObjectStorage implements ArrayAccess which means you can just read/write keys like any other array. The offset* functions are not really intended to be called directly. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 21, 2019 Author Share Posted May 21, 2019 Thanks kicken, Any recommendations of these two approaches. The second is what I meant by "Or, would it be best not to replace the data in storage..." class OptionOne { private $clientList; public function __construct() { $this->clientList = new SplObjectStorage;; } public function clientConnects($stream) { $client=new UnregisteredClient($stream); $stream->setClient($client); $this->clientList->attach($stream, new UnregisteredClient); } public function clientRegisters($stream, $data) { $client=new NewClient($stream, $data); $stream->setClient($client); $this->clientList[$stream]=$client; } } class ClientShell { private $client; public function __construct($client) {$this->setClient($client);} public function setClient($client){$this->client=$client;} public function getClient(){return $this->client;} } class OptionTwo { private $clientList; public function __construct() { $this->clientShellList = new SplObjectStorage;; } public function clientConnects($stream) { $clientShell=new ClientShell(new UnregisteredClient($stream)); $this->clientShellList->attach($stream, $clientShell); } public function clientRegisters($stream, $data) { $client=new NewClient($stream, $data); $this->clientShellList[$stream]->setClient($client); } Quote Link to comment Share on other sites More sharing options...
kicken Posted May 22, 2019 Share Posted May 22, 2019 I'd probably just go with essentially option one. I don't see any real benefit to using a shell object. class OptionOne { private $clientList; public function __construct() { $this->clientList = new SplObjectStorage;; } public function clientConnects($stream) { $client=new UnregisteredClient($stream); $stream->setClient($client); $this->clientList[$stream] = $client; } public function clientRegisters($stream, $data) { $client=new NewClient($stream, $data); $stream->setClient($client); $this->clientList[$stream] = $client; } } Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 22, 2019 Author Share Posted May 22, 2019 Thanks kicken, I've been stung trying to add too much to a class before, but think you are right and will do so. 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.