Jump to content

Recommended Posts

I come from a long line of down to earth, hard-up, and linear programmers and I've been introduced to the wonders of web. But now I'm a bit lost. Okay, I know much about programming concepts with experience with C++, C#, and assembly, but I can't seem to tame PHP. I've written some code to act as a sort of framework in the spirit of the M-V-C pattern, but I don't know what's happening to my instantiations!

 

I've been reading up on the help files, documentations, and looking up excellent code, but I still don't know what's happening. I'm totally in the dark. Can someone point me to the light?

 

I create my includes (global variables), and local variables, and even classes, but I seem to lose the instantiation of these objects when I navigate more than three pages from my index. What's happening and how do I avoid this?

Link to comment
https://forums.phpfreaks.com/topic/233608-help-understandingtaming-the-web/
Share on other sites

When you speak of

I navigate more than three pages from my index
I take it that's index.php file. And "instantiations" are those mystical mathematical logical thingies that keep errors and bugs away.  ::) Well php don't have any. When a php script ends the variables are sent to phpcoder heaven. They are gone forever.  If you need to preserve a variable like the user's name it can be saved in the $_SESSION[] variables you also can use the $_GET and $_POST for passing variables.

 

Is there a specific problem your having?

HTTP is stateless.  Your loss of information isn't due to PHP, but to the nature of the web.  The same problem exists in ASP.NET, Ruby, Python, etc.  Each request made on the server invokes a new request cycle from HTTP.  You need to store your values before making a request.  You can, as said above, store these values in sessions.  You could also use a database, a file, or even cookies (not recommended for anything non-trivial).

 

Also, coming from a C++ and C# background, you should know that this:

 

I create my includes (global variables)

 

Can be a very bad thing.  Just because PHP is lax with enforcing type and, except for the most recent version, lacks namespaces, that doesn't mean good practices should be thrown out the window.  Take care with any config variables you need, and try not to break scope with them.  And, just to nip it in the bud here, never use the 'global' keyword.

Thanks a lot everyone. It makes more sense to me now!  :D Also, I've been reading up on CodeIgniter's Docus and they're really good material for both beginners and advanced users. Something about data Routing and Output Processing gave me some clues, but Nightslyr's answers really put the spot light on.

 

Thanks again Nightslyr and sunfighter! :D

sunfighter and Nightslyr both pretty much nailed it.  I like to describe PHP as having "page scope".  A page (script) runs in the server environment, creates allocates memory/creates variables etc, executes, and destroys everything upon completion -- which is done as quickly as possible, and i the web context, is completed at the point that the HTTP response has been sent to the client.

 

If you want to persist data, you need to use files, or a database, or a memory cache, or something else that exists outside of php. 

 

Java in particular is often used due to its common support for application servers, where you can make pojos or ejb's that can exist independently of what is going on in the web layer.

 

Yes you can persist data in session variables.  This shouldn't be overused in my opinion.  Session variables have to be serialized when changed or added to (by default to files on the web server) and unserialized on every request. 

 

One important concept to understand, is that of PHP resource variables.  These are in general, handles, connections and other variables that are often used by php to provide an interface to client libraries or other external libraries.  They are special, and can not survive the serialization/unserialization process. 

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.