0

I am familiar with object oriented programming and most of my experience is with a WAMP/LAMP stack. I am curious about what happens when someone visits my site. To the point of apache responding to a user. Is an object created dynamically for each user as they do things on my website, then deleted as that action runs its course or is there an object that stays with that users' session and get cleaned up after the connection closes? Im trying to gain a deeper understanding of it.

Thanks

Ice76
  • 1,143
  • 8
  • 16
  • Each request to the server will call execution of your script, so basically nothing is stored for the next request except session data ($_SESSION). – TurtleTread Jun 02 '17 at 19:17
  • Well technically it's created any time the script is run, this can be a human visitor, a cron job, anything. You can assign objects to a SESSION variable but no, it's not automatically done. – WheatBeak Jun 02 '17 at 19:17

2 Answers2

2

Every request is unique. Apache gets a request, tells PHP to generate a response, and then returns that response. There is no persistence between any request.

As quoted from PHP Request Lifecycle

From what the article explains, the script is parsed and executed each time a request is made to the server! This just seems crazy to me!

No, that article is accurate. There are various ways of caching the results of the parsing/compilation, but the script is executed in its entirety each time. No instances of classes or static variables are retained across requests. In essence, each request gets a fresh, never-before execute copy of your application.

If you really want detailed information have a read through the page linked in that post aka 'that article' (http://php.find-info.ru/php/016/ch20lev1sec5.html). And you'll know more then I do.

small note its on the zend framework but if you read around it the core principles remain.

Community
  • 1
  • 1
Jenne
  • 864
  • 5
  • 11
  • Essentially, caching is only used per request and garbage collection cleans up any objects after the request is complete? – Ice76 Jun 02 '17 at 19:40
  • Caching is more of a complex php script converted to a result so it doesn't have to do the complex script again for the next request and thus being faster. However that is only useful if the resulting data is immutable, or at least a limited time. One example of what I work with almost every day is a template engine which parses the templates to php code and saves the php code. So the next time it only has to execute the PHP code and not parse the template as well. – Jenne Jun 02 '17 at 19:43
1

It would depend on how Apache is set up and which method is being used:

So specifically what exactly happens would slightly vary because of the aforementioned two ways of processing requests on the server. But in general it would follow this concept:

When a user requests something, the apache creates new objects for each request. Once the purpose of that object is fulfilled, the object is destroyed. Classes or instances are not retained across various different requests.

Somdip Dey
  • 3,346
  • 6
  • 28
  • 60