Welcome Guest, Not a member yet? Register   Sign In
Theoretical question
#1

[eluser]Stobyer[/eluser]
Hi, folks.
I'm new to web development and php frameworks, including codeigniter, so, please, excuse me if I'm wrong or if my question is a trivial.

As I understood from CI's code the whole framework (all needed libs, helpers, models, db, etc.) loaded for EACH http request.
Most of the above resources are dataless, so they may be reused for multiple requests. Even models may be designed such way that data will be stored apart of functionality.

So my questions are:
Why aren't dataless resources reused?
Why not the main framework components initialize only once (may be in a lazy way)?

Thanks you in advance.
Stobyer
#2

[eluser]danmontgomery[/eluser]
Welcome Stobyer,

What you're describing isn't a limitation of CodeIgniter, but of PHP... To be honest, I wouldn't call it a limitation at all.

The only way to transfer data across HTTP requests are to store the data on the server (sessions) or the client (cookies). It is technically possible to store objects in the session, but that data is accessible to the browser and there are definite security implications, not to mention the extra storage it would take to store a persistent object for each user. Cookies, besides being stored on the client's computer, have a size limitation of 4k.

Generically speaking, when an HTTP request is made, an apache (using apache as an example, it would be any webserver) instance is spawned, the appropriate PHP script is run, output is sent to the browser, and the process is killed. There are several benefits... For example, this allows for PHP to do it's own garbage collection, exponentially increases security, and it allows for hundreds or thousands of concurrent users on a relatively low-end machine, depending on the application.

Within each instance, dataless resources are reused. Each necessary part of the framework is only loaded once per request, and stored in memory... However, it's necessary that this happen for every request.

Hope that helps!
#3

[eluser]farinspace[/eluser]
Great thoughts noctrum!

Stobyer, you also have to remember that HTTP is a stateless protocol, meaning that a server sees each request as unique, like noctrum mentions: we use cookies and sessions to maintain state for a user across multiple requests.

As far as I know CI only loads what it needs to run, as you build your application you load specific lib per-request as needed. In terms of the code, it should be written to be reused, using classes, functions/methods (models, views) ...
#4

[eluser]Stobyer[/eluser]
Thank you very much, noctrum!

May be, I didn't express myself well.
I didn't mean to store the whole framework in a session, it's definitely waste of memory. What I'm asking is why should I have number of instances of URL helper for example? Why should I load this class for each request? It's just piece of code that could reside in compiled form in memory, so I can use it whenever I need.

As you said, for each HTTP request new instance of apache is spawned (BTW what do you mean by instance? thread or process?). So we have separate execution context for each request and it's just perfect for dataless components reuse.

The approach I'm talking about should improve overall memory usage and performance. Is this a PHP + web server implementation limitation?
#5

[eluser]Stobyer[/eluser]
Thanks you for an answer, farinspace.

As I wrote in my previous post, I didn't mean to save the whole framework in session. But keep dataless resources in memory for all requests from all users.

About CI, it loads what you told it to load, as I understood there is no lazy initialization. For sure it tries to load the minimum, but it loads the same form helper for each request, why isn't it just keep the code in memory. Singleton for example.




Theme © iAndrew 2016 - Forum software by © MyBB