简体   繁体   English

关于面向对象的PHP的几个问题

[英]Few questions on objects oriented PHP

Now I need to work with php , last time I work with it , it was simple procedural script . 现在我需要使用php,上一次使用它是简单的过程脚本。 Now that its support OO , what about simple web behavior for example if I initiate object in page X 现在它支持OO了,例如,如果我在X页中启动对象,那么简单的Web行为呢?

  1. Does each time the same user that refresh the page X will initiate the object all over again? 每次刷新页面X的同一用户是否都会重新发起对象?
  2. if I initiate object from class Foo in page X and in this object I set value to static variables . 如果我从X页的Foo类发起对象,并在该对象中将值设置为静态变量。 Can I in page Y get this variables values with out initiate class Foo . 我可以在Y页中获取没有初始化类Foo的此变量值吗? just get them with Foo::staticVar. 只需使用Foo :: staticVar即可获得它们。
  1. Yes. 是。
  2. No, if by page Y you mean a separate request from the client. 不,如果在第Y页表示您是来自客户端的单独请求。

The only way to persist data between page requests is to use sessions (or cookies). 在页面请求之间保留数据的唯一方法是使用会话 (或cookie)。 Any objects you've created and any changes you've made to static class variables (or fields, as they're commonly called) will be lost. 您创建的所有对象以及对静态类变量(或字段,通常称为字段)所做的任何更改都将丢失。

What you probably need to do is store some information as session data (using the $_SESSION superglobal; see the link above) and use this to initialize your objects and static fields at the beginning of each request. 您可能需要做的是将一些信息存储为会话数据(使用$_SESSION超全局变量;请参见上面的链接),并在每次请求开始时使用它来初始化对象和静态字段。

This is easily done by way of a separate PHP file that handles all your initialization for you. 这可以通过一个单独的PHP文件轻松完成,该文件可以为您处理所有初始化。 For example, you might include this at the beginning of all your scripts if you wanted to persist an object of type Foo : 例如,如果要保留类型为Foo的对象,则可以在所有脚本的开头包含以下内容:

<?php

session_start();

// Get some session data that you've previously set.
if (isset($_SESSION['foo']))
{
    $foo = $_SESSION['foo'];
}
else
{
    // Hasn't been initialized, so do so now.
    $foo = new Foo();
    $_SESSION['foo'] = $foo;
}

?>

I'd be careful about storing objects in session data in this way, though, as it's opposed to the statelessness of the HTTP protocol. 但是,以这种方式将对象存储在会话数据中时,我会格外小心,因为这与HTTP协议的无状态相反。 It's probably best to store a minimum of information in the session from which the state of the application can be reconstructed. 最好在会话中存储最少的信息,以从中可以重构应用程序的状态。 For example, you might store just the ID of the currently logged in user, rather than the whole object, and then re-initialize it from that ID on each request. 例如,您可能只存储当前登录用户的ID,而不是整个对象,然后在每次请求时从该ID重新初始化它。 As mentioned in the comments, any object that you want to persist in this way must also implement the __sleep and __wakeup methods. 如注释中所述,要以这种方式保留的任何对象还必须实现__sleep__wakeup方法。

It's probably worth reading some of the discussion on this question , too. 也可能值得阅读有关此问题的一些讨论。

  1. Yes
  2. Yes, static var must be public (not private or protected) 是的,静态var必须是公共的(非私有或受保护的)
  1. Yes, the objects are recreated upon each request. 是的,将根据每个请求重新创建对象。 A php script, even an object-oriented one has no idea of any previous requests and all variables/objects (hell even functions and classes) are parsed and created upon each request. 一个php脚本,甚至是面向对象的php脚本,都不知道任何先前的请求,并且所有变量/对象(甚至函数和类)都将根据每个请求进行解析和创建。

  2. Now this depends on what you mean by page X and page Y: supposing X and Y are php files that in some manner are both executed during the same request, then the answer is Yes, of course. 现在,这取决于页面X和页面Y的含义:假设X和Y是以某种方式在同一请求中均执行的php文件,那么答案是肯定的。 If on the other hand they denote different requests, then no, since the page Y has no idea if the request to page X has even occurred, even less about the nature of the code that was or wasn't executed. 另一方面,如果它们表示不同的请求,则否,因为页面Y甚至不知道对页面X的请求是否已经发生,甚至不知道已执行或未执行的代码的性质。

The fundamental nature of PHP hasn't changed - you still start a fresh PHP program with each HTTP request, so your objects will need to be re-instantiated with each page load. PHP的基本性质没有改变-您仍然会通过每个HTTP请求启动一个全新的PHP程序,因此需要在每次页面加载时重新实例化您的对象。

However, it is possible to avoid some of the overhead of rebuilding and reloading your data by storing a serialized copy of an object in the $_SESSION , and then loading it from the serialized copy on subsequent page loads. 但是,可以通过将对象的序列化副本存储在$_SESSION ,然后在后续页面加载时从序列化副本中加载对象来避免重建和重新加载数据的一些开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM