Jump to content

OOP Conceptual Question


n2963p

Recommended Posts

I've been using PHP procedural programming for some years and do rather well.  I'm trying to upgrade my skills to include Object Oriented Programming, but I am having a hard time with one concept.  All of the texts seem to limit their examples to it's use on a single page.  They don't cover creating an instance on one page and using it on a subsequent page.  When I was learning to use MySQL, the texts never mentioned that every time you move from one page to another, you must reconnect to the database.  I fought that problem for some time before I found a reference that explained it.  Now with OOP instances, do they disappear when you move from one page to another (I believe they do), and what is the common solution to that issue.

 

Thanks in advance.

Link to comment
Share on other sites

Yes, they disappear too. The exception is sessions.

 

HTTP is stateless: it does not keep track of the state between requests. PHP does not know what happened on the last page. That means every script has to start from scratch: there aren't any variables or classes or database connections from before.

The solution is to have your scripts load what they need. Need a class defined? require_once() the file. Need a database connection? Create one. Need a value? Look it up.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.