Jump to content

Accessing same array in different objects


NotionCommotion

Recommended Posts

I have the following scenario.  I was thinking of having logic in Controller with potentially closure returned from Request and Response to accomplish it.
 
class Main
{
    public function __construct() 
    {
        $this->clientList = new \SplObjectStorage();        
    }
    private function addClient($connStream)
    {
        $obj=new ClientController(new Request(),new Response());
        $this->clientList[$connStream]->clientController=new ClientController(new Request(),new Response());;
    }
}

class ClientController
{
    public $callbacks=[];
    public function __construct($request, $response)
    {
        $this->request=$request;
        $this->response=$response;
    }
    public function m1(){$this->callbacks[]=123;}  //Needs to add, remove, read elements on $this->callbacks
    public function m2(){$this->request->foo();}   //Needs to add, remove, read elements on ClientController::callbacks
    public function m3(){$this->response->bar();}  //Needs to add, remove, read elements on ClientController::callbacks
}

I now realize I am going about it wrong.  I leaning towards Option 2 but maybe Option 1.  Any thoughts?  Or maybe something totally different?  Thanks 

 

Option 1

private function addClient()
{
    $callbacks=[];
    $this->clientList[$connStream]->clientController=new ClientController(new Request($callbacks),new Response($callbacks),$callbacks);
}

class ClientController
{
    public function __construct($request, $response, &$callbacks) 
    {
        $this->request=$request;
        $this->response=$response;
        $this->callbacks=$callbacks;        
    }
}
Option 2
private function addClient()
{
    $callbacks=new Callbacks();
    $this->clientList[$connStream]->clientController=new ClientController(new Request($callbacks),new Response($callbacks),$callbacks);
}

class ClientController
{
    public function __construct($request, $response, $callbacks) 
    {
        $this->request=$request;
        $this->response=$response;
        $this->callbacks=$callbacks;        
    }
}

Option 3

private function addClient()
{
    $this->clientList[$connStream]->clientController=new ClientController(new Request(),new Response(),new Callbacks());
}

class ClientController
{
    public function __construct($request, $response, $callbacks) 
    {
        $this->request=$request;
        $request->callbacks=$callbacks;
        $this->response=$response;
        $request->response=$callbacks;
        $this->callbacks=$callbacks;        
    }
}

Option 4

private function addClient()
{
    $this->clientList[$connStream]->clientController=new ClientController(new Callbacks());
}

class ClientController
{
    public function __construct($callbacks) 
    {
        $this->request=new Request($callbacks);
        $this->response=new Response($callbacks);
        $this->callbacks=$callbacks;        
    }
}

 

Link to comment
Share on other sites

I'm not clear on what you're trying to get out of this. Why do the callbacks need to be shared between objects? Can't you put the callbacks in one place and expose collection-type methods to let others read or modify them?

I would rather put them one place and allow the other objects to read and modify them, but which place?  Within ClientController, I could certainly do so regardless of where they are located, but how can both Request and Response do so unless I passed them within their methods?
 
Also, what do you mean by a collection-type method?   Not http://php.net/manual/en/class.ds-collection.php, right?
Link to comment
Share on other sites

By a collection I mean programming's general concept of a collection. Such classes almost always include methods like "add" or "remove" or "search". Then there's some variability stacked ( :D) on top, like there are read-only collections, and there are collections that support adding/removing in multiple ways.

SplObjectStorage is such a class.

 

I can't quite remember what you need the callbacks for but the set of them should be in one place, authoritatively. That probably means the first place they're defined - Main, in your example. Now "main" isn't really an entity so it probably shouldn't contain entity state (like a set of callbacks) so maybe that's why none of those options seems right to you.

 

IIRC these callbacks are tied to a connection, right? Each one is only useful for that connection, and only useful while that connection exists. That means the callbacks should be "stored" in there, be that inside the connection class itself or inside a "collection" class for the callbacks which the connection class holds onto.

 

All of which I'm saying without really understanding when and where you might be adding or removing callbacks.

 

class Connection {

	private $callbacks = [];

	// public function addCallback(...) { }
	// public function removeCallback(...) { }

}
class Connection {

	public $callbacks = null;

	public function __construct() { $this->callbacks = new CallbacksCollection(); }

}

class CallbacksCollection {

	private $callbacks = [];

	// public function add(...) { }
	// public function remove(...) { }

}
If that's right then these can be viewed more like events than callbacks - if they aren't, in fact, actually representative of events. So perhaps structuring your code around an event model would make more sense.
Link to comment
Share on other sites

Thanks requinix, what you are showing is pretty much what I am doing.    Once difference is within the connection, I am adding two objects ConnectionRequest and ConnectionResponse which need to access the CallbacksCollection.  But being different objects, they don't have access to one another, nor to the parent Connection object.
class ClientCollection
{
      public function __construct() {$this->clientList = new \SplObjectStorage();}
      public function add() {
           $callbacksCollection=new CallbacksCollection();
           return new Connection(
               $callbacksCollection,
               new ConnectionRequest($callbacksCollection),
               new ConnectionResponse($callbacksCollection)
           );
     }
}

 

Link to comment
Share on other sites

What are they doing with the callbacks? Would it make more sense to have them reference the connection (with callbacks) rather than the callbacks directly?

 
Yes, it makes more sense to have them with the connection.   But how can they access them?
class CallbacksCollection
{
    public function __construct($connectionRequest, $connectionResponse, $callbacksCollection)
    {
        $this->connectionRequest = $connectionRequest;
        $this->connectionResponse = $connectionResponse;
        $this->callbacksCollection = $callbacksCollection;
    }
    public function bla()
    {
        $x=$this->callbacksCollection;    //have access
        $this->connectionRequest->blabla();
        $this->connectionResponse->blabla();
    }
}

class ConnectionRequest
{
    public function blabla()
    {
        //How do I access the callbacksCollection
    }
}
class ConnectionResponse
{
    public function blabla()
    {
        //How do I access the callbacksCollection
    }
}
Link to comment
Share on other sites

If the callbacks are part of the connection, and the connection requests and responses are part of the connection, then put the callbacks into the connection and tell the requests and responses what connection to use.

// connection
$this->callbacks = whatever;
$this->request = new ConnectionRequest($this);
$this->response = new ConnectionResponse($this);
Then the request/responses uses the callbacks however the connection class decides to expose them. Maybe just as a public property.
Link to comment
Share on other sites

Guess I was just getting caught up on trying to inject the other classes at Connection's creation.  I will do instead as you show.  THanks

class ClientCollection
{
    public function __construct() 
    {
        $this->clientList = new \SplObjectStorage();        
    }
    public function add($connStream)
    {
        $obj=new Connection($connStream, new ConnectionRequest(), new ConnectionResponse(), new CallbacksCollection());
    }
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.