Reputation: 4966
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.
Upvotes: 3
Views: 149
Reputation: 198219
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.
Upvotes: 3
Reputation: 36
The object lives only as long as the single page load does. After that, everything is destroyed. So, any information that you need to re-assemble that object (e.g. primary key) needs to come back to you through the next HTTP request (often, by submitting it in a form).
Upvotes: 0
Reputation: 522597
Basically yes, you'll recreate the object. In PHP objects are instantiated and killed a lot more than you might do in other languages. You could serialize the object into the session, but to stay RESTful, you should not make requests depend on the state of the session. Also, the post may have changed or vanished from the database in the meantime, so you should check against the database again anyway. Just pass enough information through the HTML form/URL/request to recreate the state you need to update the post in the database (that usually means to pass along the post id).
Upvotes: 0
Reputation: 27077
I think part of the problem is that you're thinking as though everything should be an object. In PHP, you construct objects where necessary. I would suggest that treating the POST
data as an object is not intuitive in PHP; you could do it, but you'll come to the problems you suggest of having to define everything. On the other hand, I find that defining user
and session
objects can simplify your code and make using it more intuitive.
Upvotes: 0
Reputation: 1045
In PHP, everything disapperas when a page load is over. The regular post (as in HTTP-post) is targeted on a specific file, a post handler you can say. In the post, it would be good to get the id of the forum post that have been edited. Get the current version from the database by the id, update the object with the edited data and then save it.
Upvotes: 0