I am an experienced programmer and have used OOP a lot, so I'm very familiar with the concept. I'm also experienced in web development. What I'm not familiar with is how they go together. For example, say I am writing some forum software. I create a class that will handle and represent a "Post" by the user. I'll create methods that load it from the database, methods that display it on the screen to read, and methods to allow the user to edit the content.
This is where my confusion comes it. I'll instruct the Post object to load the content from the database then display it in a form for the user to edit. Where will the form submit its information? How do I send the information back to the Post object if it no longer exists on the server? Do I have to recreate the post object, feed it the form data, then execute the update method? Is there some way to make objects persistent on the server until they're no longer needed?
I apologize if my questions don't make a lot of sense. If I knew what to ask Google would have already answered my questions.
To deal with that problem, there is a popular pattern in OOP that's called MVC (Model View Controller). Give it some reads for the theory and read some of the implementations around.
This is where my confusion comes it. I'll instruct the Post object to load the content from the database then display it in a form for the user to edit. Where will the form submit its information?
To your webserver via HTTP.
How do I send the information back to the Post object if it no longer exists on the server?
From a persistent store, often this is a database.
Do I have to recreate the post object, feed it the form data, then execute the update method?
Yes, because of the stateless nature of a HTTP request that commands your application as the toplevel request.
Is there some way to make objects persistent on the server until they're no longer needed?
Sure. There are object "freezers" that allow you to store objects persistently and if you don't need them any longer, you remove them from the fridge.
A PHP framework that has a MVC to offer is symfony2. It has some explanation how that MVC works in this article: Symfony2 versus Flat PHP which explains pretty well how MVC can work with any HTTP based PHP script/application.
The sourcecode is located on github if you prefer reading code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With