how to do this using PHP OOP to maintain object state in different pages.
如何使用PHP OOP在不同的页面中维护对象状态。
the problem is im always instantiate the object on every page.
问题是我总是在每个页面上实例化对象。
is there a solution that i instantiate it once and maintain its object on different pages.
是否有一个解决方案,我实例化它一次,并在不同的页面上维护它的对象。
Thanks in advance
提前致谢
2 个解决方案
#1
In PHP pretty much everything is instantiated on every page hit. If you want to maintain state you have a number of choices:
在PHP中,几乎所有内容都在每个页面上实例化。如果你想维持状态,你有很多选择:
- For user-specific data you can put it in a cookie (not recommended for security reasons);
- Put user-specific data in the session, which basically means writing it to file and loading it from file on every hit;
- Storing it in some form of persistent storage, such as a file or database table;
- Store it in a cache of some kind (eg memcached).
对于特定于用户的数据,您可以将其放入cookie中(出于安全原因不建议使用);
将特定于用户的数据放入会话中,这基本上意味着将其写入文件并在每次点击时从文件加载;
将其存储在某种形式的持久存储中,例如文件或数据库表;
将其存储在某种缓存中(例如memcached)。
Which one you use depends on factors such as if the data is global, user-specific, etc and a number of other factors (such as how often it is read, how often it is written, etc).
您使用哪一个取决于诸如数据是全局的,用户特定的等因素以及许多其他因素(例如读取频率,写入频率等)。
So it's impossible to give you a definitive answer as the nature of what you want to be persistent is unclear. If you're worried about the cost of creating an object then, unless it is really expensive, don't. Don't optimize a problem until you have a problem.
所以不可能给你一个明确的答案,因为你想要持久的性质尚不清楚。如果你担心创建一个对象的成本,那么,除非它真的很贵,否则不要。在遇到问题之前不要优化问题。
#2
You can also look at Object serialization. You will find more information here. http://uk.php.net/serialize. This will convert your object into a format that can be used to store into session data. Once your data is stored in sessions, you can then read the data on the next user page load and unserialize the data to get back your object!
您还可以查看对象序列化。您可以在此处找到更多信息。 http://uk.php.net/serialize。这会将您的对象转换为可用于存储到会话数据的格式。一旦您的数据存储在会话中,您就可以在下一个用户页面加载时读取数据并反序列化数据以获取对象!
Here is a good article which shows how serialization will allow your object to be used across two different PHP pages. http://www.phpbuilder.com/manual/en/language.oop.serialization.php
这是一篇很好的文章,展示了序列化如何允许您的对象跨两个不同的PHP页面使用。 http://www.phpbuilder.com/manual/en/language.oop.serialization.php
#1
In PHP pretty much everything is instantiated on every page hit. If you want to maintain state you have a number of choices:
在PHP中,几乎所有内容都在每个页面上实例化。如果你想维持状态,你有很多选择:
- For user-specific data you can put it in a cookie (not recommended for security reasons);
- Put user-specific data in the session, which basically means writing it to file and loading it from file on every hit;
- Storing it in some form of persistent storage, such as a file or database table;
- Store it in a cache of some kind (eg memcached).
对于特定于用户的数据,您可以将其放入cookie中(出于安全原因不建议使用);
将特定于用户的数据放入会话中,这基本上意味着将其写入文件并在每次点击时从文件加载;
将其存储在某种形式的持久存储中,例如文件或数据库表;
将其存储在某种缓存中(例如memcached)。
Which one you use depends on factors such as if the data is global, user-specific, etc and a number of other factors (such as how often it is read, how often it is written, etc).
您使用哪一个取决于诸如数据是全局的,用户特定的等因素以及许多其他因素(例如读取频率,写入频率等)。
So it's impossible to give you a definitive answer as the nature of what you want to be persistent is unclear. If you're worried about the cost of creating an object then, unless it is really expensive, don't. Don't optimize a problem until you have a problem.
所以不可能给你一个明确的答案,因为你想要持久的性质尚不清楚。如果你担心创建一个对象的成本,那么,除非它真的很贵,否则不要。在遇到问题之前不要优化问题。
#2
You can also look at Object serialization. You will find more information here. http://uk.php.net/serialize. This will convert your object into a format that can be used to store into session data. Once your data is stored in sessions, you can then read the data on the next user page load and unserialize the data to get back your object!
您还可以查看对象序列化。您可以在此处找到更多信息。 http://uk.php.net/serialize。这会将您的对象转换为可用于存储到会话数据的格式。一旦您的数据存储在会话中,您就可以在下一个用户页面加载时读取数据并反序列化数据以获取对象!
Here is a good article which shows how serialization will allow your object to be used across two different PHP pages. http://www.phpbuilder.com/manual/en/language.oop.serialization.php
这是一篇很好的文章,展示了序列化如何允许您的对象跨两个不同的PHP页面使用。 http://www.phpbuilder.com/manual/en/language.oop.serialization.php