开发者

PHP Beginner: Where and how are objects stored?

开发者 https://www.devze.com 2023-04-09 22:43 出处:网络
In an app written in PHP (e.g., a social network), let\'s say that 10 users (signed-in) are browsing the website.

In an app written in PHP (e.g., a social network), let's say that 10 users (signed-in) are browsing the website.

In PHP code, there is "user" object created to store users data and to pass values to other functions and classes.

Question: When these 10 users go to user.php, which has code to create "user" object, how are these objects stored in memory in PHP? Do they not conflict? Is each one of the "user" objects are uniquely stored in the memory or would one be overwritten by another?

For example, user a visits first so object "user" contains his/her data but when user second vi开发者_如何学编程sits, the "user" object in memory is overwritten so when first user calls the object, it's the second users data retrieved.

Or, is it unique?

I want to understand object in PHP as a newbie, please explain it simply because none of the web pages I found regarding OOP explains this.


PHP is a CGI application, that means, it's being started and terminated on each request.

  • a client sends a request to the web server
  • the server starts PHP and passes the request to it
  • PHP allocates a chunk of memory for your script
  • your script is being executed, all objects it creates are stored in that chunk of memory
  • you script generates some html, this html is sent to the client
  • the memory is being freed and PHP is stopped

If you have 10 clients requests coming at the same time, 10 copies of php will be started and 10 independent memory chunks will be used. So, no, objects from different requests do not interfere.

(Note: this explanation is deliberately simplified, there are actually different php setups and persistence options).


The best way to learn this is to install php on a local PC or Mac and then create a php info file

<?php
phpinfo();
?>

... then open it in your browser...This will show you all the settings on your server for php and other things.

Regarding the answer to your question, it's a bit more of an advanced topic for a newbee, but php sessions are what do the work of keeping user info. They usually work off a session id which is unique to the user for a small amount of time, and they dynamically allocate memory or disk space/flat files or a database (again see the settings above) to store the relevant data.

Unfortunately for you none of this is "automatic" you have to create the scripts to make it happen and behave in the way you want. Asking questions on this site is a good start...


You need to look at object design patterns in relation to php which is quite a big subject in its own right. There is an excellent Apress book called 'PHP Objects, Patterns and Practice' which explains some of the more common patterns and how you might use them and would be a good place to start learning.


The users information is all stored in a database, the user object will have to retrieve this data each time the page loads.

The object know what user is looking at the page because of their session_id, which in a nut shell is a random id given to you, stored in a cookie.

using the session_id you can retrieve the correct information form the database.

0

精彩评论

暂无评论...
验证码 换一张
取 消