0

Now I need to work with php , last time I work with it , it was simple procedural script . Now that its support OO , what about simple web behavior for example if I initiate object in page X

  1. Does each time the same user that refresh the page X will initiate the object all over again?
  2. if I initiate object from class Foo in page X and in this object I set value to static variables . Can I in page Y get this variables values with out initiate class Foo . just get them with Foo::staticVar.
Gordon
  • 312,688
  • 75
  • 539
  • 559
user63898
  • 29,839
  • 85
  • 272
  • 514

4 Answers4

3
  1. Yes.
  2. No, if by page Y you mean a separate request from the client.

The only way to persist data between page requests is to use sessions (or cookies). 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.

This is easily done by way of a separate PHP file that handles all your initialization for you. For example, you might include this at the beginning of all your scripts if you wanted to persist an object of type 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. 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. As mentioned in the comments, any object that you want to persist in this way must also implement the __sleep and __wakeup methods.

It's probably worth reading some of the discussion on this question, too.

Community
  • 1
  • 1
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
  • 1
    If you want to store objects, the class needs to implement the __sleep and __wakeup functions. See php.net/__sleep and php.net/__wakeup for more information. – Stijn Leenknegt Dec 08 '10 at 09:41
1
  1. Yes
  2. Yes, static var must be public (not private or protected)
r92
  • 2,800
  • 2
  • 19
  • 24
0
  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.

  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. 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.

Ramuns Usovs
  • 1,474
  • 11
  • 10
0

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.

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307