0

I use my PHP (like normal) in a server architecture where I have an incoming request and build an outgoing response. All request attributes (like client ID) are given to each sub function and class to make magic with (like in my Mappers and Models and Helpers).

Is there an idea to work with request data WITHOUT putting it from function to function to function.

Idea 1: Make my Request/Response object static. I have exactly one request and one response so there is no problem to do this.

Idea 2: Saving data in Session (or similar). This also sounds uncomfortable and heavy.

Is there another idea doing this?

David Pauli
  • 89
  • 2
  • 12
  • 1
    *"I have exactly one request and one response"* – not a big fan of unit testing I take it…? Explicitly passing information is the most maintainable and flexible way to do anything. Is it *really* such a burden…? – deceze May 09 '17 at 06:31

1 Answers1

1

Implement your own Request or RequestContext class and populate a object of this type with all the data that ongoing methods might need. This way you don't need a growing number of arguments for each of your functions, instead you just forward the entire Request.

This is much easier to extend and you don't suffer drawbacks of "global" data. It's a typical pattern which is used by a lot of frameworks as well.

towe75
  • 1,470
  • 10
  • 9
  • *"Make my Request/Response object static"* – I assume that means the OP already has such a thing, but that's somewhat unclear… – deceze May 09 '17 at 06:41
  • Well, hard to tell without example code. But it's a best practice approach anyways, so i posted it. – towe75 May 09 '17 at 07:02
  • Its no question about solving specific example code, but finding an idea how to implement (or refactor) such case. A _AbstractRequest_ and _AbstactResponse_ is a object you have exactly one time (in my case). So using it in a _static_ context cames directly in my head. Putting the whole object in every function like suggested sounds like the best idea. – David Pauli May 09 '17 at 11:18