SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast cliffgs's Avatar
    Join Date
    Aug 2003
    Location
    West Coast, New Zealand
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question about how long an object is active

    Hi

    I have a site which uses an index.php as a controller, and a couple of view pages (such as search.php and form.php) which are both 'included' via the index.php. So my question is, if I have an object instantiated in index.php is it in operation in any number of pages, provided they are all included from the index.php file that created the object.

    A simple question, perhaps, but one which I really would like to be sure of the answer.

    I must say, I like using an index file and then 'including' other files, a technique which I learned while reading Kevin Yank's books.

    Many thanks

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Well, this would be a case of "there's the rule, and then there's all the exceptions."

    An object by default would only be in existence during a single execution. (Read: Until you get to the final line of index.php, in this example). Garbage collection then cleans everything up.

    HOWEVER;
    An object instantiated into the $_SESSION global array would (provided index.php invoked session_start() ) exist for the duration of the session.
    An object passed as a cookie (bad idea, but possible) would persist for the lifetime of the cookie. (Actually it wouldnt - it'd get passed back and forth repeatedly, with the object's server-local existence being destroyed by garbage cleanup as usual)


    Including is treated (for the most part) as though you took the contents of the included file, and inserted them at that point. Variables are in-scope, functions, etc. (The reverse is also true, if the included page defines variables or functions, they are available in the global scope of the file that did the including.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict bronze trophy Hall of Famer's Avatar
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    370
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)
    Well as far as I know, the object is available for the lifetime of the HTTP request, although for temporary objects created in methods they will not be accessible after method call completes. You can manually destroy the object by unsetting it, I seldom do this myself though. If you are simply including files, objects included in these files should be available to you during the current HTTP request. Be aware that including files with global data is not a good practice, you should only include class or function source files instead. Many IDEs wont even be able to recognize variables/objects defined in an included file, a good example is Netbeans, but it helps since I end up refactoring my application so that no variable is loaded by including files.

    Alternatively you can store an object in a session so it will be available throughout a user session, which ends with the user closing the browser or manually destroyed by the internal script. This way the object will be active even after you refresh the page, but since PHP session vars cannot store objects you have to play the serialize/unserialize trick all the time. Sessions can be useful, but over-relying on session is also a poor programming practice. A session var exists only if it must exist, so use this feature sparingly. For cookies, I only use it to store user login info such as user id and username, its not safe so do not even use it for password.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    Well, this would be a case of "there's the rule, and then there's all the exceptions."

    An object by default would only be in existence during a single execution. (Read: Until you get to the final line of index.php, in this example). Garbage collection then cleans everything up.

    HOWEVER;
    An object instantiated into the $_SESSION global array would (provided index.php invoked session_start() ) exist for the duration of the session.
    An object passed as a cookie (bad idea, but possible) would persist for the lifetime of the cookie. (Actually it wouldnt - it'd get passed back and forth repeatedly, with the object's server-local existence being destroyed by garbage cleanup as usual)


    Including is treated (for the most part) as though you took the contents of the included file, and inserted them at that point. Variables are in-scope, functions, etc. (The reverse is also true, if the included page defines variables or functions, they are available in the global scope of the file that did the including.)
    There's nothing wrong with storing an object in a session. The exception to this is storing a resource in an object, you can't for example store a db connection in an object and pass that object to the next request, not that you'd want to)
    According to the docs, classes' destruct class is called when it is no longer used, but usually winds up being until end of execution (test by playing with the magic __destruct() function.

  5. #5
    SitePoint Enthusiast cliffgs's Avatar
    Join Date
    Aug 2003
    Location
    West Coast, New Zealand
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Well, this would be a case of "there's the rule, and then there's all the exceptions."

    An object by default would only be in existence during a single execution. (Read: Until you get to the final line of index.php, in this example). Garbage collection then cleans everything up.

    HOWEVER;
    An object instantiated into the $_SESSION global array would (provided index.php invoked session_start() ) exist for the duration of the session.
    An object passed as a cookie (bad idea, but possible) would persist for the lifetime of the cookie. (Actually it wouldnt - it'd get passed back and forth repeatedly, with the object's server-local existence being destroyed by garbage cleanup as usual)


    Including is treated (for the most part) as though you took the contents of the included file, and inserted them at that point. Variables are in-scope, functions, etc. (The reverse is also true, if the included page defines variables or functions, they are available in the global scope of the file that did the including.)
    Thanks for that. At least now I know the direction my searching and learning must go in. I can find out how to instantiate an object into the $_SESSION array. I didn't know you could do that, but then I should have known, it seems you can do most anything with PHP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •